<commit_before>"""
 SlipStream Client
 =====
 Copyright (C) 2013 SixSq Sarl (sixsq.com)
 =====
 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at
 
      http://www.apache.org/licenses/LICENSE-2.0
 
 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
"""

import base64
import commands
import os
import socket
import time

from stratuslab.ConfigHolder import ConfigHolder as StratuslabConfigHolder
from stratuslab.marketplace.ManifestDownloader import ManifestDownloader
from stratuslab.Creator import Creator
from stratuslab.Creator import CreatorBaseListener
from stratuslab.vm_manager.Runner import Runner
from stratuslab.volume_manager.volume_manager_factory import VolumeManagerFactory

from slipstream.cloudconnectors.BaseCloudConnector import BaseCloudConnector
import slipstream.exceptions.Exceptions as Exceptions
import slipstream.util as util
from slipstream.utils.ssh import generateSshKeyPair


def getConnector(configHolder):
    return getConnectorClass()(configHolder)


def getConnectorClass():
    return StratuslabClientCloud


class StratuslabClientCloud(BaseCloudConnector):
    RUNINSTANCE_RETRY_TIMEOUT = 3

    cloudName = 'stratuslab'

    def __init__(self, slipstreamConfigHolder):
        super(StratuslabClientCloud, self).__init__(slipstreamConfigHolder)

        self.slConfigHolder = StratuslabConfigHolder(slipstreamConfigHolder.options,
                                                     slipstreamConfigHolder.config)
        self.listener = CreatorBaseListener(verbose=(self.verboseLevel > 1))
        
        self.setCapabilities(contextualization=True, 
                             direct_ip_assignment=True,
                             orchestrator_can_kill_itself_or_its_vapp=True)

    def startImage(self, user_info, image_info):
        return self.getVmsDetails()

    def _buildImage(self, userInfo, imageInfo):

        self._prepareMachineForBuildImage()

        self.slConfigHolder.set('marketplaceEndpoint',
                                userInfo.get_cloud('marketplace.endpoint'))

        manifestDownloader = ManifestDownloader(self.slConfigHolder)

        imageId = self.getImageId(imageInfo)
        imageInfo['imageVersion'] = manifestDownloader.getImageVersion(imageId=imageId)

        self._updateStratuslabConfigHolderForBuildImage(userInfo, imageInfo)

        creator = Creator(imageId, self.slConfigHolder)
        creator.setListener(self.listener)

        createImageTemplateDict = creator._getCreateImageTemplateDict()
        msgData = StratuslabClientCloud._getCreateImageTemplateMessaging(imageInfo,
                                                      self._getCloudInstanceName())

        def ourCreateTemplateDict():
            createImageTemplateDict.update(msgData)
            return createImageTemplateDict

        creator._getCreateImageTemplateDict = ourCreateTemplateDict

        creator.create()

        # 
        # if messaging is set to 'pdisk', then try polling for the new image
        # identifier from the storage system; otherwise will just return empty
        # string
        #
        self._newImageId = self._pollStorageForNewImage(self.slConfigHolder)

    def _pollStorageForNewImage(self, slConfigHolder):

        newImageId = ''

        msg_type = os.environ.get('SLIPSTREAM_MESSAGING_TYPE', None)
        msg_endpoint = os.environ.get('SLIPSTREAM_MESSAGING_ENDPOINT', None)

        if msg_type and msg_endpoint:
            if msg_type == 'pdisk':

                diid = os.environ.get('SLIPSTREAM_DIID', None)
                if diid:
                    tag = "SlipStream-%s" % diid
                    filters = {'tag': [tag,]}

                    slConfigHolder.set('pdiskEndpoint', msg_endpoint)

                    pdisk = VolumeManagerFactory.create(slConfigHolder)

                    print "Searching on %s for disk with tag %s." % (msg_endpoint, tag)

                    # hardcoded polling for 30' at 1' intervals
                    for i in range(30):
                        print "Search iteration %d" % i
                        volumes = pdisk.describeVolumes(filters)
                        if len(volumes) > 0:
                            try:
                                newImageId = volumes[0]['identifier']<commit_msg>flush output so that it is seen in the log file.<commit_after>"""
 SlipStream Client
 =====
 Copyright (C) 2013 <NAME> (six<EMAIL>)
 =====
 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at
 
      http://www.apache.org/licenses/LICENSE-2.0
 
 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
"""

import base64
import commands
import os
import socket
import sys
import time

from stratuslab.ConfigHolder import ConfigHolder as StratuslabConfigHolder
from stratuslab.marketplace.ManifestDownloader import ManifestDownloader
from stratuslab.Creator import Creator
from stratuslab.Creator import CreatorBaseListener
from stratuslab.vm_manager.Runner import Runner
from stratuslab.volume_manager.volume_manager_factory import VolumeManagerFactory

from slipstream.cloudconnectors.BaseCloudConnector import BaseCloudConnector
import slipstream.exceptions.Exceptions as Exceptions
import slipstream.util as util
from slipstream.utils.ssh import generateSshKeyPair


def getConnector(configHolder):
    return getConnectorClass()(configHolder)


def getConnectorClass():
    return StratuslabClientCloud


class StratuslabClientCloud(BaseCloudConnector):
    RUNINSTANCE_RETRY_TIMEOUT = 3

    cloudName = 'stratuslab'

    def __init__(self, slipstreamConfigHolder):
        super(StratuslabClientCloud, self).__init__(slipstreamConfigHolder)

        self.slConfigHolder = StratuslabConfigHolder(slipstreamConfigHolder.options,
                                                     slipstreamConfigHolder.config)
        self.listener = CreatorBaseListener(verbose=(self.verboseLevel > 1))
        
        self.setCapabilities(contextualization=True, 
                             direct_ip_assignment=True,
                             orchestrator_can_kill_itself_or_its_vapp=True)

    def startImage(self, user_info, image_info):
        return self.getVmsDetails()

    def _buildImage(self, userInfo, imageInfo):

        self._prepareMachineForBuildImage()

        self.slConfigHolder.set('marketplaceEndpoint',
                                userInfo.get_cloud('marketplace.endpoint'))

        manifestDownloader = ManifestDownloader(self.slConfigHolder)

        imageId = self.getImageId(imageInfo)
        imageInfo['imageVersion'] = manifestDownloader.getImageVersion(imageId=imageId)

        self._updateStratuslabConfigHolderForBuildImage(userInfo, imageInfo)

        creator = Creator(imageId, self.slConfigHolder)
        creator.setListener(self.listener)

        createImageTemplateDict = creator._getCreateImageTemplateDict()
        msgData = StratuslabClientCloud._getCreateImageTemplateMessaging(imageInfo,
                                                      self._getCloudInstanceName())

        def ourCreateTemplateDict():
            createImageTemplateDict.update(msgData)
            return createImageTemplateDict

        creator._getCreateImageTemplateDict = ourCreateTemplateDict

        creator.create()

        # 
        # if messaging is set to 'pdisk', then try polling for the new image
        # identifier from the storage system; otherwise will just return empty
        # string
        #
        self._newImageId = self._pollStorageForNewImage(self.slConfigHolder)

    def _pollStorageForNewImage(self, slConfigHolder):

        newImageId = ''

        msg_type = os.environ.get('SLIPSTREAM_MESSAGING_TYPE', None)
        msg_endpoint = os.environ.get('SLIPSTREAM_MESSAGING_ENDPOINT', None)

        if msg_type and msg_endpoint:
            if msg_type == 'pdisk':

                diid = os.environ.get('SLIPSTREAM_DIID', None)
                if diid:
                    tag = "SlipStream-%s" % diid
                    filters = {'tag': [tag,]}

                    slConfigHolder.set('pdiskEndpoint', msg_endpoint)

                    pdisk = VolumeManagerFactory.create(slConfigHolder)

                    print >> sys.stdout, "Searching on %s for disk with tag %s." % (msg_endpoint, tag)
                    sys.stdout.flush()

                    # hardcoded polling for 30' at 1' intervals
                    for i in range(30):
                        print >> sys.stdout, "Search iteration %d" % i
                        sys.stdout.flush()
                        volumes = pdisk.describeVolumes(filters)
                        if len(volumes) > 0:
                            try:
                                newImageId = volumes[0]['identifier']
{'fraction_non_alphanumeric': 0.06773203513599323, 'fraction_numerical': 0.0040215895861995974, 'mean_word_length': 2.6430223592906708, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 18, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '263145', 'n_tokens_mistral': 2442, 'n_tokens_neox': 2329, 'n_words': 610}