Пример #1
0
    def __init__(
            self, answers, APP, nodeps=False, update=False, target_path=None,
            dryrun=False, answers_format=ANSWERS_FILE_SAMPLE_FORMAT, **kwargs):
        self.dryrun = dryrun
        self.kwargs = kwargs

        app = APP  # FIXME

        self.nulecule_base = Nulecule_Base(
            nodeps, update, target_path, dryrun, answers_format)

        if os.path.exists(app):
            logger.info("App path is %s, will be populated to %s", app, target_path)
            app = self._loadApp(app)
        else:
            logger.info("App name is %s, will be populated to %s", app, target_path)

        printStatus("Loading app %s ." % app)
        if not target_path:
            if self.nulecule_base.app_path:
                self.nulecule_base.target_path = self.nulecule_base.app_path
            else:
                self.nulecule_base.target_path = os.getcwd()

        self.utils = Utils(self.nulecule_base.target_path)

        self.nulecule_base.app = app

        self.answers_file = answers
        self.docker_cli = Utils.getDockerCli(self.dryrun)
Пример #2
0
    def checkArtifacts(self, component, check_provider=None):
        checked_providers = []
        artifacts = self.getArtifacts(component)
        if not artifacts:
            logger.debug("No artifacts for %s", component)
            return []

        for provider, artifact_list in artifacts.iteritems():
            if (check_provider and not provider == check_provider) \
                    or provider in checked_providers:
                continue

            logger.debug("Provider: %s", provider)
            for artifact in artifact_list:
                if "inherit" in artifact:
                    self._checkInherit(component, artifact["inherit"], checked_providers)
                    continue
                path = os.path.join(self.target_path, Utils.sanitizePath(artifact))
                if os.path.isfile(path):
                    printStatus("Artifact %s: OK." % (artifact))
                else:
                    printErrorStatus("Missing artifact %s." % (artifact))
                    raise Exception("Missing artifact %s (%s)" % (artifact, path))
            checked_providers.append(provider)

        return checked_providers
Пример #3
0
    def __init__(self,
                 answers,
                 APP,
                 dryrun=False,
                 debug=False,
                 stop=False,
                 answers_format=ANSWERS_FILE_SAMPLE_FORMAT,
                 **kwargs):

        self.debug = debug
        self.dryrun = dryrun
        self.stop = stop
        self.kwargs = kwargs

        if "answers_output" in kwargs:
            self.answers_output = kwargs["answers_output"]

        if os.environ and "IMAGE" in os.environ:
            self.app_path = APP
            APP = os.environ["IMAGE"]
            del os.environ["IMAGE"]
        elif "image" in kwargs:
            logger.warning("Setting image to %s" % kwargs["image"])

            self.app_path = APP
            APP = kwargs["image"]
            del kwargs["image"]

        self.kwargs = kwargs

        if APP and os.path.exists(APP):
            self.app_path = APP
        else:
            if not self.app_path:
                self.app_path = os.getcwd()
            install = Install(answers,
                              APP,
                              dryrun=dryrun,
                              target_path=self.app_path,
                              answers_format=answers_format)
            install.install()
            printStatus("Install Successful.")

        self.nulecule_base = Nulecule_Base(target_path=self.app_path,
                                           dryrun=dryrun,
                                           file_format=answers_format)
        if "ask" in kwargs:
            self.nulecule_base.ask = kwargs["ask"]

        workdir = None
        if "workdir" in kwargs:
            workdir = kwargs["workdir"]

        self.utils = Utils(self.app_path, workdir)
        if "workdir" not in kwargs:
            kwargs["workdir"] = self.utils.workdir

        self.answers_file = answers
        self.plugin = Plugin()
        self.plugin.load_plugins()
Пример #4
0
    def __init__(
            self, answers, APP, nodeps=False, update=False, target_path=None,
            dryrun=False, answers_format=ANSWERS_FILE_SAMPLE_FORMAT, **kwargs):
        self.dryrun = dryrun
        self.kwargs = kwargs

        app = APP  # FIXME

        self.nulecule_base = Nulecule_Base(
            nodeps, update, target_path, dryrun, answers_format)

        if os.path.exists(app):
            logger.info("App path is %s, will be populated to %s", app, target_path)
            app = self._loadApp(app)
        else:
            logger.info("App name is %s, will be populated to %s", app, target_path)

        printStatus("Loading app %s ." % app)
        if not target_path:
            if self.nulecule_base.app_path:
                self.nulecule_base.target_path = self.nulecule_base.app_path
            else:
                self.nulecule_base.target_path = os.getcwd()

        self.utils = Utils(self.nulecule_base.target_path)

        self.nulecule_base.app = app

        self.answers_file = answers
        self.docker_cli = Utils.getDockerCli(self.dryrun)
Пример #5
0
    def _processComponent(self, component, graph_item):
        logger.debug(
            "Processing component '%s' and graph item '%s'", component, graph_item)
        provider_class = self.plugin.getProvider(self.nulecule_base.provider)
        dst_dir = os.path.join(self.utils.workdir, component)
        if self.stop:
            component_values = self.nulecule_base.getValues(component, skip_asking=True)
        else:
            component_values = self.nulecule_base.getValues(component)
        provider = provider_class(component_values, dst_dir, self.dryrun)
        if provider:
            printStatus("Deploying component %s ..." % component)
            logger.info("Using provider %s for component %s",
                        self.nulecule_base.provider, component)
        else:
            raise Exception("Something is broken - couldn't get the provider")

        provider.artifacts, dst_dir = self._processArtifacts(component, provider)

        try:
            provider.init()
            if self.stop:
                provider.undeploy()
            else:
                provider.deploy()
        except ProviderFailedException as ex:
            printErrorStatus(ex)
            logger.error(ex)
            raise
Пример #6
0
    def checkArtifacts(self, component, check_provider=None):
        checked_providers = []
        artifacts = self.getArtifacts(component)
        if not artifacts:
            logger.debug("No artifacts for %s", component)
            return []

        for provider, artifact_list in artifacts.iteritems():
            if (check_provider and not provider == check_provider) \
                    or provider in checked_providers:
                continue

            logger.debug("Provider: %s", provider)
            for artifact in artifact_list:
                if "inherit" in artifact:
                    self._checkInherit(component, artifact["inherit"],
                                       checked_providers)
                    continue
                path = os.path.join(self.target_path,
                                    Utils.sanitizePath(artifact))
                if os.path.isfile(path):
                    printStatus("Artifact %s: OK." % (artifact))
                else:
                    printErrorStatus("Missing artifact %s." % (artifact))
                    raise Exception("Missing artifact %s (%s)" %
                                    (artifact, path))
            checked_providers.append(provider)

        return checked_providers
Пример #7
0
    def _copyFromContainer(self, image):
        image = self.nulecule_base.getImageURI(image)

        name = "%s-%s" % (self.utils.getComponentName(image), ''.join(
            random.sample(string.letters, 6)))
        logger.debug("Creating a container with name %s", name)

        # Workaround docker bug BZ1252168 by using run instead of create
        create = [
            self.docker_cli, "run", "--name", name, "--entrypoint",
            "/bin/true", image
        ]
        logger.debug(" ".join(create))
        subprocess.call(create)
        cp = [
            self.docker_cli, "cp",
            "%s:/%s" % (name, APP_ENT_PATH), self.utils.tmpdir
        ]
        logger.debug(cp)
        if not subprocess.call(cp):
            logger.debug("Application entity data copied to %s",
                         self.utils.tmpdir)

        printStatus("Copied app successfully.")
        rm = [self.docker_cli, "rm", name]
        subprocess.call(rm)
Пример #8
0
    def __init__(
            self, answers, APP, dryrun=False, debug=False, stop=False,
            answers_format=ANSWERS_FILE_SAMPLE_FORMAT, **kwargs):

        self.debug = debug
        self.dryrun = dryrun
        self.stop = stop
        self.kwargs = kwargs
        self.cli_provider = None

        if "answers_output" in kwargs:
            self.answers_output = kwargs["answers_output"]

        if "cli_provider" in kwargs:
            self.cli_provider = kwargs["cli_provider"]

        if os.environ and "IMAGE" in os.environ:
            self.app_path = APP
            APP = os.environ["IMAGE"]
            del os.environ["IMAGE"]
        elif "image" in kwargs:
            logger.warning("Setting image to %s" % kwargs["image"])

            self.app_path = APP
            APP = kwargs["image"]
            del kwargs["image"]

        self.kwargs = kwargs

        if APP and os.path.exists(APP):
            self.app_path = APP
        else:
            if not self.app_path:
                self.app_path = os.getcwd()
            install = Install(
                answers, APP, dryrun=dryrun, target_path=self.app_path,
                answers_format=answers_format)
            install.install()
            printStatus("Install Successful.")

        self.nulecule_base = Nulecule_Base(
            target_path=self.app_path,
            dryrun=dryrun,
            file_format=answers_format,
            cli_provider=self.cli_provider)
        if "ask" in kwargs:
            self.nulecule_base.ask = kwargs["ask"]

        workdir = None
        if "workdir" in kwargs:
            workdir = kwargs["workdir"]

        self.utils = Utils(self.app_path, workdir)
        if "workdir" not in kwargs:
            kwargs["workdir"] = self.utils.workdir

        self.answers_file = answers
        self.plugin = Plugin()
        self.plugin.load_plugins()
Пример #9
0
    def install(self):
        answerContent = self.nulecule_base.loadAnswers(self.answers_file)
        printAnswerFile(json.dumps(answerContent))

        mainfile_dir = self.nulecule_base.app_path
        if not self.dryrun:
            if self._fromImage():
                self.nulecule_base.pullApp()
                self._copyFromContainer(self.nulecule_base.app)
                mainfile_dir = self.utils.getTmpAppDir()

            current_app_id = None
            if os.path.isfile(self.nulecule_base.getMainfilePath()):
                current_app_id = Utils.getAppId(self.nulecule_base.getMainfilePath())
                printStatus("Loading app_id %s ." % current_app_id)

            if current_app_id:
                tmp_mainfile_path = os.path.join(mainfile_dir, MAIN_FILE)
                self.nulecule_base.loadMainfile(tmp_mainfile_path)
                logger.debug("%s path for pulled image: %s", MAIN_FILE, tmp_mainfile_path)
                if current_app_id != self.nulecule_base.app_id:
                    raise Exception("You are trying to overwrite existing app %s with app %s - clear or change current directory." 
                                                % (current_app_id, self.nulecule_base.app_id))
        elif self._fromImage():
            logger.warning("Using DRY-RUN together with install from image may result in unexpected behaviour")

        if self.nulecule_base.update or (not self.dryrun and not os.path.exists(self.nulecule_base.getMainfilePath())):
            if self._fromImage():
                self._populateApp()
            else:
                logger.info("Copying content of directory %s to %s", self.nulecule_base.app_path, self.nulecule_base.target_path)
                self._populateApp(src=self.nulecule_base.app_path)

        mainfile_path = os.path.join(self.nulecule_base.target_path, MAIN_FILE)
        if not self.nulecule_base.mainfile_data:
            self.nulecule_base.loadMainfile(mainfile_path)

        logger.debug("App ID: %s", self.nulecule_base.app_id)

        self.nulecule_base.checkSpecVersion()
        printStatus("Checking all artifacts")
        self.nulecule_base.checkAllArtifacts()

        printStatus("Loading Nulecule file.")
        values = {}
        if not self.nulecule_base.nodeps:
            logger.info("Installing dependencies for %s", self.nulecule_base.app_id)
            values = self._installDependencies()
            printStatus("All dependencies installed successfully.")

        logger.debug(values)
        answerContent = self.nulecule_base.loadAnswers(values)
        logger.debug(self.nulecule_base.answers_data)
        if self.nulecule_base.write_sample_answers:
            self.nulecule_base.writeAnswersSample()

        printAnswerFile(json.dumps(answerContent))
        printStatus("Install Successful.")
        return None
Пример #10
0
    def checkAllArtifacts(self):
        for graph_item in self.mainfile_data["graph"]:
            component = graph_item.get("name")
            if not component:
                raise ValueError("Component name missing in graph")

            checked_providers = self.checkArtifacts(component)
            printStatus("All artifacts OK. ")
            logger.info("Artifacts for %s present for these providers: %s", component, ", ".join(checked_providers))
Пример #11
0
    def checkAllArtifacts(self):
        for graph_item in self.mainfile_data["graph"]:
            component = graph_item.get("name")
            if not component:
                raise ValueError("Component name missing in graph")

            checked_providers = self.checkArtifacts(component)
            printStatus("All artifacts OK. ")
            logger.info("Artifacts for %s present for these providers: %s",
                        component, ", ".join(checked_providers))
Пример #12
0
    def _copyFromContainer(self, image):
        image = self.nulecule_base.getImageURI(image)

        name = "%s-%s" % (self.utils.getComponentName(image), ''.join(random.sample(string.letters, 6)))
        logger.debug("Creating a container with name %s", name)

        create = [self.docker_cli, "create", "--name", name, image, "nop"]
        logger.debug(" ".join(create))
        subprocess.call(create)
        cp = [self.docker_cli, "cp", "%s:/%s" % (name, APP_ENT_PATH), self.utils.tmpdir]
        logger.debug(cp)
        if not subprocess.call(cp):
            logger.debug("Application entity data copied to %s", self.utils.tmpdir)

        printStatus("Copied app successfully.")
        rm = [self.docker_cli, "rm", name]
        subprocess.call(rm)
Пример #13
0
    def _copyFromContainer(self, image):
        image = self.nulecule_base.getImageURI(image)

        name = "%s-%s" % (self.utils.getComponentName(image),
                          ''.join(Utils.getUniqueUUID()))
        logger.debug("Creating a container with name %s", name)

        # Workaround docker bug BZ1252168 by using run instead of create
        create = [self.docker_cli, "run", "--name", name, "--entrypoint", "/bin/true", image]
        logger.debug(" ".join(create))
        subprocess.call(create)
        cp = [self.docker_cli, "cp", "%s:/%s" % (name, APP_ENT_PATH), self.utils.tmpdir]
        logger.debug(cp)
        if not subprocess.call(cp):
            logger.debug("Application entity data copied to %s", self.utils.tmpdir)

        printStatus("Copied app successfully.")
        rm = [self.docker_cli, "rm", name]
        subprocess.call(rm)
Пример #14
0
    def pullApp(self, image = None, update = None):
        if not image:
            image = self.app
        if not update:
            update = self.update

        image = self.getImageURI(image)
        if not update:
            check_cmd = ["docker", "images", "-q", image]
            image_id = subprocess.check_output(check_cmd)
            logger.debug("Output of docker images cmd: %s", image_id)
            if len(image_id) != 0:
                logger.debug("Image %s already present with id %s. Use --update to re-pull.", image, image_id.strip())
                return

        pull = ["docker", "pull", image]
        printStatus("Pulling image %s ..." % image)
        if subprocess.call(pull) != 0:
            printErrorStatus("Couldn't pull %s." % image)
            raise Exception("Couldn't pull %s" % image)
Пример #15
0
    def _installDependencies(self):
        values = {}
        for graph_item in self.nulecule_base.mainfile_data["graph"]:
            component = graph_item.get("name")
            if not component:
                raise ValueError("Component name missing in graph")

            if not self.utils.isExternal(graph_item):
                values[component] = self.nulecule_base.getValues(component, skip_asking = True)
                logger.debug("Component %s is part of the app", component)
                logger.debug("Values: %s", values)
                continue

            logger.info("Component %s is external dependency", component)

            image_name = self.utils.getSourceImage(graph_item)
            component_path = self.utils.getExternalAppDir(component)
            mainfile_component_path = os.path.join(component_path, MAIN_FILE)
            logger.debug("Component path: %s", component_path)
            if not os.path.isfile(mainfile_component_path) or self.nulecule_base.update:
                printStatus("Pulling %s ..." % image_name)
                component_app = Install(self.nulecule_base.answers_data, image_name, self.nulecule_base.nodeps, 
                                        self.nulecule_base.update, component_path, self.dryrun)
                values = Utils.update(values, component_app.install())
                printStatus("Component %s installed successfully."%component)
                logger.debug("Component installed into %s", component_path)
            else:
                printStatus("Component %s already installed."%component)
                logger.info("Component %s already exists at %s - remove the directory or use --update option", component, component_path)

        return values
Пример #16
0
    def pullApp(self, image=None, update=None):
        if not image:
            image = self.app
        if not update:
            update = self.update

        image = self.getImageURI(image)
        if not update:
            check_cmd = ["docker", "images", "-q", image]
            image_id = subprocess.check_output(check_cmd)
            logger.debug("Output of docker images cmd: %s", image_id)
            if len(image_id) != 0:
                logger.debug(
                    "Image %s already present with id %s. Use --update to re-pull.",
                    image, image_id.strip())
                return

        pull = ["docker", "pull", image]
        printStatus("Pulling image %s ..." % image)
        if subprocess.call(pull) != 0:
            printErrorStatus("Couldn't pull %s." % image)
            raise Exception("Couldn't pull %s" % image)
Пример #17
0
    def _installDependencies(self):
        values = {}
        for graph_item in self.nulecule_base.mainfile_data["graph"]:
            component = graph_item.get("name")
            if not component:
                raise ValueError("Component name missing in graph")

            if not self.utils.isExternal(graph_item):
                values[component] = self.nulecule_base.getValues(
                    component, skip_asking=True)
                logger.debug("Component %s is part of the app", component)
                logger.debug("Values: %s", values)
                continue

            logger.info("Component %s is external dependency", component)

            image_name = self.utils.getSourceImage(graph_item)
            component_path = self.utils.getExternalAppDir(component)
            mainfile_component_path = os.path.join(component_path, MAIN_FILE)
            logger.debug("Component path: %s", component_path)
            if not os.path.isfile(
                    mainfile_component_path) or self.nulecule_base.update:
                printStatus("Pulling %s ..." % image_name)
                component_app = Install(self.nulecule_base.answers_data,
                                        image_name, self.nulecule_base.nodeps,
                                        self.nulecule_base.update,
                                        component_path, self.dryrun)
                component_app.install()
                values = Utils.update(values,
                                      component_app.answers_file_values)
                printStatus("Component %s installed successfully." % component)
                logger.debug("Component installed into %s", component_path)
            else:
                printStatus("Component %s already installed." % component)
                logger.info(
                    "Component %s already exists at %s - remove the directory "
                    "or use --update option" % (component, component_path))

        return values
Пример #18
0
def deal():
    """ Returns void

    Deals cards, and checks for splitting
    """

    while True:
        try:
            wager = int(input("Place wager: "))
            if 1 <= wager <= balance:
                break
            else:
                print(
                    "!! Invalid Choice!! Please pick a number between 1 and %d"
                    % balance)
        except ValueError:
            print("!! Invalid Choice!! Please pick a number between 1 and %d" %
                  balance)

    deck = Deck()
    player = Hand()
    dealer = Hand()

    # deal two cards each for player and dealer
    for i in range(2):
        player.addCard(deck.dealCard())
        dealer.addCard(deck.dealCard())

    card_1 = player.getCard(0)
    card_2 = player.getCard(1)
    upcard = dealer.getCard(0)
    utils.printStatus(upcard, player)

    # check for splitting
    if player.canSplit():
        while True:
            choice = input("Would you like to Split (Y / N): ").upper()
            if choice == "Y":
                if balance < wager * 2:
                    print("Inadequate balance :(")
                else:
                    print("Splitting into two hands. \nHand #1")
                    player = Hand()
                    player.addCard(card_1)
                    player.addCard(deck.dealCard())
                    utils.printStatus(upcard, player)
                    engine(player, dealer, deck, wager)

                    # checks for adequate balance
                    if balance >= wager:
                        print("Hand #2")
                        player = Hand()
                        dealer = Hand()
                        player.addCard(card_2)
                        player.addCard(deck.dealCard())

                        for i in range(2):
                            dealer.addCard(deck.dealCard())

                        upcard = dealer.getCard(0)
                        utils.printStatus(upcard, player)
                        engine(player, dealer, deck, wager)
                    else:
                        print("Inadequate balance. Unable to play Hand #2 :(")
                    break
            elif choice == "N":
                engine(player, dealer, deck, wager)
                break
            else:
                print("!! Invalid Choice !! Please choose one of (Y / N)")
    else:
        engine(player, dealer, deck, wager)
Пример #19
0
import requests, constants, utils, spinner, time
from bs4 import BeautifulSoup
from getpass import getpass

# Constants
spinner = spinner.Spinner()
url = constants.Url()
session_req = requests.session()

credentials = {'userid': '', 'pwd': '', 'request_id': '1103954795815962967'}

# Login
credentials['userid'] = input("Email: ")
credentials['pwd'] = getpass("Password: ")

spinner.start('Logging in')
res = session_req.post(url.LOGIN,
                       data=credentials,
                       headers=dict(referer=url.LOGIN))
spinner.stop()
utils.printStatus(res)
Пример #20
0
    def install(self):
        answerContent = self.nulecule_base.loadAnswers(self.answers_file)
        printAnswerFile(json.dumps(answerContent))

        mainfile_dir = self.nulecule_base.app_path
        if not self.dryrun:
            if self._fromImage():
                self.nulecule_base.pullApp()
                self._copyFromContainer(self.nulecule_base.app)
                mainfile_dir = self.utils.getTmpAppDir()

            current_app_id = None
            if os.path.isfile(self.nulecule_base.getMainfilePath()):
                current_app_id = Utils.getAppId(
                    self.nulecule_base.getMainfilePath())
                printStatus("Loading app_id %s ." % current_app_id)

            if current_app_id:
                tmp_mainfile_path = os.path.join(mainfile_dir, MAIN_FILE)
                self.nulecule_base.loadMainfile(tmp_mainfile_path)
                logger.debug("%s path for pulled image: %s", MAIN_FILE,
                             tmp_mainfile_path)
                if current_app_id != self.nulecule_base.app_id:
                    msg = ("You are trying to overwrite existing app %s with "
                           "app %s - clear or change current directory." %
                           (current_app_id, self.nulecule_base.app_id))
                    raise Exception(msg)
        elif self._fromImage():
            logger.warning("Using DRY-RUN together with install from image "
                           "may result in unexpected behaviour")

        if self.nulecule_base.update or \
            (not self.dryrun
             and not os.path.exists(self.nulecule_base.getMainfilePath())):
            if self._fromImage():
                self._populateApp()
            else:
                logger.info("Copying content of directory %s to %s",
                            self.nulecule_base.app_path,
                            self.nulecule_base.target_path)
                self._populateApp(src=self.nulecule_base.app_path)

        mainfile_path = os.path.join(self.nulecule_base.target_path, MAIN_FILE)
        if not self.nulecule_base.mainfile_data:
            self.nulecule_base.loadMainfile(mainfile_path)

        logger.debug("App ID: %s", self.nulecule_base.app_id)

        self.nulecule_base.checkSpecVersion()
        printStatus("Checking all artifacts")
        self.nulecule_base.checkAllArtifacts()

        printStatus("Loading Nulecule file.")
        if not self.nulecule_base.nodeps:
            logger.info("Installing dependencies for %s",
                        self.nulecule_base.app_id)
            self.answers_file_values = self._installDependencies()
            printStatus("All dependencies installed successfully.")

        logger.debug(self.answers_file_values)
        answerContent = self.nulecule_base.loadAnswers(
            self.answers_file_values)
        logger.debug(self.nulecule_base.answers_data)
        if self.nulecule_base.write_sample_answers:
            self.nulecule_base.writeAnswersSample()

        printAnswerFile(json.dumps(answerContent))
        printStatus("Install Successful.")
        return None