Beispiel #1
0
    def run(self, config, args):
        envy = Envy(config)

        if envy.ip():
            host_string = '%s@%s' % (envy.remote_user, envy.ip())

            temp_tar = tempfile.NamedTemporaryFile(delete=True)

            with fabric.api.settings(host_string=host_string):
                if args.files:
                    dotfiles = args.files.split(', ')
                else:
                    dotfiles = config['defaults']['dotfiles'].split(', ')

                with tarfile.open(temp_tar.name, 'w') as archive:
                    for dotfile in dotfiles:
                        path = os.path.expanduser('~/%s' % dotfile)
                        if os.path.exists(path):
                            if not os.path.islink(path):
                                archive.add(path, arcname=dotfile)

                fabric.operations.put(temp_tar, '~/dotfiles.tar')
                fabric.operations.run('tar -xvf ~/dotfiles.tar')
        else:
            logging.error('Could not find IP to upload file to.')
Beispiel #2
0
    def run(self, config, args):
        envy = Envy(config)

        if envy.ip():
            host_string = '%s@%s' % (envy.remote_user, envy.ip())

            with fabric.api.settings(host_string=host_string):
                file_list = [(os.path.expanduser(filename), location) for
                             filename, location in
                             envy.project_config.get('files', {}).iteritems()]

                for filename, endlocation in file_list:
                    logging.info("Putting file from '%s' to '%s'",
                                 filename, endlocation)

                    for i in range(24):
                        try:
                            fabric.operations.put(filename, endlocation,
                                                  mirror_local_mode=True,
                                                  use_sudo=True)
                            break
                        except fabric.exceptions.NetworkError:
                            logging.debug('Unable to upload the file ' \
                                          'from `%s`. Your ENVy is ' \
                                          'probably still booting. Trying '\
                                          'again in 10 seconds.' % filename)
                            time.sleep(10)

        else:
            logging.error('Could not determine IP.')
Beispiel #3
0
def envy_dotfiles(args):
    """Upload user dotfiles from local machine."""
    config = _get_config(args)

    # if user defines -n in cli, append name to project name.

    if args.name:
        config['project_config']['name'] = '%s-%s' % (
            config['project_config']['name'], args.name)

    envy = Envy(config)

    if envy.ip():
        host_string = '%s@%s' % (envy.remote_user, envy.ip())

        temp_tar = tempfile.NamedTemporaryFile(delete=True)

        with fabric.api.settings(host_string=host_string):
            dotfiles = ['.vimrc', '.gitconfig', '.gitignore',
                        '.screenrc']

            with tarfile.open(temp_tar.name, 'w') as archive:
                for dotfile in dotfiles:
                    path = os.path.expanduser('~/%s' % dotfile)
                    if os.path.exists(path):
                        tarinfo = tarfile.TarInfo(name=dotfile)
                        tarinfo.mtime = time.time()
                        tarinfo.mode = 0755
                        archive.addfile(tarinfo, fileobj=open(path))

            fabric.operations.put(temp_tar, '~/dotfiles.tar')
            fabric.operations.run('tar -xvf ~/dotfiles.tar')
    else:
        logging.error('Could not find IP to upload file to.')
Beispiel #4
0
def envy_dotfiles(args):
    """Upload user dotfiles from local machine."""
    config = _get_config(args)

    # if user defines -n in cli, append name to project name.

    if args.name:
        config['project_config']['name'] = '%s-%s' % (
            config['project_config']['name'], args.name)

    envy = Envy(config)

    if envy.ip():
        host_string = '%s@%s' % (envy.remote_user, envy.ip())

        temp_tar = tempfile.NamedTemporaryFile(delete=True)

        with fabric.api.settings(host_string=host_string):
            dotfiles = ['.vimrc', '.gitconfig', '.gitignore', '.screenrc']

            with tarfile.open(temp_tar.name, 'w') as archive:
                for dotfile in dotfiles:
                    path = os.path.expanduser('~/%s' % dotfile)
                    if os.path.exists(path):
                        tarinfo = tarfile.TarInfo(name=dotfile)
                        tarinfo.mtime = time.time()
                        tarinfo.mode = 0755
                        archive.addfile(tarinfo, fileobj=open(path))

            fabric.operations.put(temp_tar, '~/dotfiles.tar')
            fabric.operations.run('tar -xvf ~/dotfiles.tar')
    else:
        logging.error('Could not find IP to upload file to.')
Beispiel #5
0
    def run(self, config, args):
        envy = Envy(config)

        if envy.ip():
            host_string = "%s@%s" % (envy.remote_user, envy.ip())

            temp_tar = tempfile.NamedTemporaryFile(delete=True)

            with fabric.api.settings(host_string=host_string):
                if args.files:
                    dotfiles = args.files.split(",")
                else:
                    dotfiles = config["defaults"]["dotfiles"].split(",")

                dotfiles = [dotfile.strip() for dotfile in dotfiles]

                with tarfile.open(temp_tar.name, "w") as archive:
                    for dotfile in dotfiles:
                        path = os.path.expanduser("~/%s" % dotfile)
                        if os.path.exists(path):
                            if not os.path.islink(path):
                                archive.add(path, arcname=dotfile)

                fabric.operations.put(temp_tar, "~/dotfiles.tar")
                fabric.operations.run("tar -xvf ~/dotfiles.tar")
        else:
            logging.error("Could not determine IP.")
Beispiel #6
0
    def run(self, config, args):
        envy = Envy(config)

        if envy.ip():
            disable_known_hosts = "-o UserKnownHostsFile=/dev/null" " -o StrictHostKeyChecking=no"
            fabric.operations.local("ssh %s %s@%s" % (disable_known_hosts, envy.remote_user, envy.ip()))
        else:
            logging.error("Could not find IP.")
Beispiel #7
0
    def run(self, config, args):
        envy = Envy(config)

        if not envy.server():
            logging.error('ENVy is not running. Try running `envy up` first?')
        elif envy.ip():
            print envy.ip()
        else:
            logging.error('Could not find IP.')
Beispiel #8
0
    def run(self, config, args):
        envy = Envy(config)

        if not envy.server():
            logging.error('ENVy is not running.')
        elif envy.ip():
            print envy.ip()
        else:
            logging.error('Could not determine IP.')
Beispiel #9
0
    def run(self, config, args):
        envy = Envy(config)
        envys = []
        servers = envy.list_servers()

        for server in servers:
            if len(server.name.split(envy.name)) > 1:
                envys.append(str(server.name))
        print "ENVys for your project: %s" % str(envys)
Beispiel #10
0
    def run(self, config, args):
        envy = Envy(config)

        if envy.ip():
            host_string = '%s@%s' % (envy.remote_user, envy.ip())
            with fabric.api.settings(host_string=host_string):
                fabric.operations.run(args.command)
        else:
            logging.error('Unable to run command on ENVy, perhaps its not '
                          'booted yet?.')
Beispiel #11
0
    def run(self, config, args):
        envy = Envy(config)

        if envy.ip():
            host_string = '%s@%s' % (envy.remote_user, envy.ip())

            with fabric.api.settings(host_string=host_string):
                fabric.operations.put(args.source, args.target)
        else:
            logging.error('Could not find IP to upload file to.')
Beispiel #12
0
def envy_snapshot(args, name=None):
    """Create a snapshot of a running server."""
    config = _get_config(args)

    # if user defines -n in cli, append name to project name.
    if args.name:
        config['project_config']['name'] = '%s-%s' % (
            config['project_config']['name'], args.name)

    envy = Envy(config)
    envy.snapshot(name or ('%s-snapshot' % envy.name))
Beispiel #13
0
def envy_list(args):
    """List all ENVys in context of your current project"""
    config = _get_config(args)
    envy = Envy(config)
    foo = envy.list_servers()

    envys = []
    for server in foo:
        if len(server.name.split(envy.name)) > 1:
            envys.append(str(server.name))
    print "ENVys for your project: %s" % str(envys)
Beispiel #14
0
def envy_snapshot(args, name=None):
    """Create a snapshot of a running server."""
    config = _get_config(args)

    # if user defines -n in cli, append name to project name.
    if args.name:
        config['project_config']['name'] = '%s-%s' % (
            config['project_config']['name'], args.name)

    envy = Envy(config)
    envy.snapshot(name or ('%s-snapshot' % envy.name))
Beispiel #15
0
def envy_list(args):
    """List all ENVys in context of your current project"""
    config = _get_config(args)
    envy = Envy(config)
    foo = envy.list_servers()

    envys = []
    for server in foo:
        if len(server.name.split(envy.name)) > 1:
            envys.append(str(server.name))
    print "ENVys for your project: %s" % str(envys)
Beispiel #16
0
    def run(self, config, args):
        envy = Envy(config)

        if envy.find_server():
            envy.delete_server()
            logging.info('Deletion of ENVy \'%s\' was triggered.' % envy.name)
            while envy.find_server():
                logging.info("... still waiting")
            logging.info("Done!")

        else:
            logging.error('Could not find ENVy named \'%s\'.' % envy.name)
Beispiel #17
0
def envy_up(args):
    """Create a server and show its IP."""
    config = _get_config(args)

    # if user defines -n in cli, append name to project name.
    if args.name:
        config['project_config']['name'] = '%s-%s' % (
            config['project_config']['name'], args.name)

    envy = Envy(config)
    if not envy.server():
        logging.info('Building environment.')
        try:
            envy.build_server()
        except exceptions.ImageNotFound:
            logging.error('Could not find image.')
            return
        except exceptions.NoIPsAvailable:
            logging.error('Could not find free IP.')
            return
    if envy.auto_provision:
        envy_provision(args)
    if envy.ip():
        print envy.ip()
    else:
        print 'Environment has no IP.'
Beispiel #18
0
def envy_provision(args):
    """Manually provision a remote environment using a userdata script."""
    config = _get_config(args)

    # if user defines -n in cli, append name to project name.
    if args.name:
        config['project_config']['name'] = '%s-%s' % (
            config['project_config']['name'], args.name)

    envy = Envy(config)
    logging.info('Provisioning %s environment...' %
                 config['project_config']['name'])

    if args.remote_user:
        config['project_config']['remote_user'] = args.remote_user
    if args.userdata:
        config['project_config']['provision_script_path'] = args.userdata

    remote_user = config['project_config']['remote_user']
    try:
        provision_script_path = config['project_config'][
            'provision_script_path']
    except KeyError:
        raise SystemExit('Please specify which provision script should be used'
                         ' by passing in `-u` to the provision command, or by '
                         'defining `provision_script_path` in ./Envyfile')
    remote_provision_script_path = '~/provision_script'

    logging.info('Using userdata from: %s', provision_script_path)

    with fabric.api.settings(host_string=envy.ip(),
                             user=remote_user,
                             forward_agent=True,
                             disable_known_hosts=True):
        for i in range(12):
            try:
                fabric.operations.put(provision_script_path,
                                      remote_provision_script_path,
                                      mode=0755)
                break
            except fabric.exceptions.NetworkError:
                logging.error("Unable to upload file. Your cloud instance is "
                              "probably not yet built. Trying again in 10 "
                              "seconds.")
                time.sleep(10)

        fabric.operations.run(remote_provision_script_path)
    logging.info('...done.')
Beispiel #19
0
    def run(self, config, args):
        envy = Envy(config)

        logging.info('Running provision scripts for the `%s` ENVy...' %
                     envy.project_config['name'])
        if envy.ip():
            with fabric.api.settings(host_string=envy.ip(), user=envy.remote_user,
                                     forward_agent=True, disable_known_hosts=True):

                if args.scripts:
                    scripts = [os.path.expanduser(script) for
                               script in args.scripts]
                elif 'provision_scripts' in envy.project_config:
                    scripts = [os.path.expanduser(script) for script in
                               envy.project_config['provision_scripts']]
                elif 'provision_script_path' in envy.project_config:
                    provision_script = envy.project_config['provision_script_path']
                    scripts = [os.path.expanduser(provision_script)]
                else:
                    raise SystemExit('Please specify the path to your provision '
                                     'script(s) by either using the `--scripts` '
                                     'flag, or by defining the `provision_scripts`'
                                     ' config option in your Envyfile')

                for script in scripts:
                    logging.info('Running provision script from: %s', script)

                    for i in range(24):
                        try:
                            path = script
                            filename = os.path.basename(script)
                            remote_path = '~/%s' % filename
                            fabric.operations.run('if [ -e "$HOME/%s" ]; '
                                                  'then rm ~/%s; fi' %
                                                  (filename, filename))
                            fabric.operations.put(path, remote_path, mode=0755)
                            fabric.operations.run(remote_path)
                            break
                        except fabric.exceptions.NetworkError:
                            logging.error('Unable to upload the provision script '
                                          'from `%s`. Your ENVy is probably still '
                                          'booting. Trying again in 10 seconds.')
                            time.sleep(10)
                    logging.info('The provision script from `%s` has finished '
                                 'running.' % path)
        else:
            logging.error('Could not find IP.')
Beispiel #20
0
def envy_provision(args):
    """Manually provision a remote environment using a userdata script."""
    config = _get_config(args)

    # if user defines -n in cli, append name to project name.
    if args.name:
        config['project_config']['name'] = '%s-%s' % (
            config['project_config']['name'], args.name)

    envy = Envy(config)
    logging.info('Provisioning %s environment...' %
                 config['project_config']['name'])

    if args.remote_user:
        config['project_config']['remote_user'] = args.remote_user
    if args.userdata:
        config['project_config']['provision_script_path'] = args.userdata

    remote_user = config['project_config']['remote_user']
    try:
        provision_script_path = config['project_config']['provision_script_path']
    except KeyError:
        raise SystemExit('Please specify which provision script should be used'
                         ' by passing in `-u` to the provision command, or by '
                         'defining `provision_script_path` in ./Envyfile')
    remote_provision_script_path = '~/provision_script'

    logging.info('Using userdata from: %s', provision_script_path)

    with fabric.api.settings(host_string=envy.ip(),
                             user=remote_user,
                             forward_agent=True,
                             disable_known_hosts=True):
        for i in range(12):
            try:
                fabric.operations.put(provision_script_path,
                                      remote_provision_script_path,
                                      mode=0755)
                break
            except fabric.exceptions.NetworkError:
                logging.error("Unable to upload file. Your cloud instance is "
                              "probably not yet built. Trying again in 10 "
                              "seconds.")
                time.sleep(10)

        fabric.operations.run(remote_provision_script_path)
    logging.info('...done.')
Beispiel #21
0
    def run(self, config, args):
        envy = Envy(config)

        if envy.ip():
            disable_known_hosts = ('-o UserKnownHostsFile=/dev/null'
                                   ' -o StrictHostKeyChecking=no')
            forward_agent = '-o ForwardAgent=yes'

            options = [disable_known_hosts]
            if envy.forward_agent:
                options.append(forward_agent)

            fabric.operations.local('ssh %s %s@%s' % (' '.join(options),
                                                      envy.remote_user,
                                                      envy.ip()))
        else:
            logging.error('Could not determine IP.')
Beispiel #22
0
    def run(self, config, args):
        envy = Envy(config)

        if not envy.server():
            logging.info('Triggering ENVy boot.')
            try:
                envy.build_server()
            except exceptions.ImageNotFound:
                logging.error('Could not find image.')
                return
            except exceptions.NoIPsAvailable:
                logging.error('Could not find available IP.')
                return
        if not args.no_provision and 'provision_scripts' in envy.project_config:
            try:
                EnvyProvision().run(config, args)
            except SystemExit:
                raise SystemExit('You have not specified any provision '
                                 'scripts in your Envyfile. '
                                 'If you would like to run your ENVy '
                                 'without a provision script; use the '
                                 '`--no-provision` command line flag.')
        if envy.ip():
            print envy.ip()
        else:
            logging.error('Could not determine IP.')
Beispiel #23
0
def envy_up(args):
    """Create a server and show its IP."""
    config = _get_config(args)

    # if user defines -n in cli, append name to project name.
    if args.name:
        config['project_config']['name'] = '%s-%s' % (
            config['project_config']['name'], args.name)

    envy = Envy(config)
    if not envy.server():
        logging.info('Building environment.')
        try:
            envy.build_server()
        except exceptions.ImageNotFound:
            logging.error('Could not find image.')
            return
        except exceptions.NoIPsAvailable:
            logging.error('Could not find free IP.')
            return
    if envy.auto_provision:
        envy_provision(args)
    if envy.ip():
        print envy.ip()
    else:
        print 'Environment has no IP.'
Beispiel #24
0
def envy_ssh(args):
    """SSH into the current server."""
    config = _get_config(args)

    # if user defines -n in cli, append name to project name.
    if args.name:
        config['project_config']['name'] = '%s-%s' % (
            config['project_config']['name'], args.name)

    envy = Envy(config)
    remote_user = config['project_config']['remote_user']
    if envy.ip():
        disable_known_hosts = ('-o UserKnownHostsFile=/dev/null'
                               ' -o StrictHostKeyChecking=no')
        fabric.operations.local('ssh %s %s@%s' %
                                (disable_known_hosts, remote_user, envy.ip()))
    else:
        logging.error('Could not find IP.')
Beispiel #25
0
def envy_ip(args):
    """Show the IP of the current server."""
    config = _get_config(args)

     # if user defines -n in cli, append name to project name.
    if args.name:
        config['project_config']['name'] = '%s-%s' % (
            config['project_config']['name'], args.name)

    envy = Envy(config)

    if not envy.server():
        logging.error('Environment has not been created.\n'
                      'Try running `envy up` first?')
    elif envy.ip():
        print envy.ip()
    else:
        logging.error('Could not find IP.')
Beispiel #26
0
def envy_scp(args):
    """SCP Files to your ENVy"""
    config = _get_config(args)

    # if user defines -n in cli, append name to project name.
    if args.name:
        config['project_config']['name'] = '%s-%s' % (
            config['project_config']['name'], args.name)

    envy = Envy(config)

    if envy.ip():
        remote_user = '******'
        host_string = '%s@%s' % (remote_user, envy.ip())

        with fabric.api.settings(host_string=host_string):
            fabric.operations.put(args.source, args.target)
    else:
        logging.error('Could not find IP to upload file to.')
Beispiel #27
0
def envy_destroy(args):
    """Power-off and destroy the current server."""
    config = _get_config(args)

    # if user defines -n in cli, append name to project name.
    if args.name:
        config['project_config']['name'] = '%s-%s' % (
            config['project_config']['name'], args.name)

    envy = Envy(config)
    logging.info('Triggering environment deletion.')
    if envy.find_server():
        envy.delete_server()
        while envy.find_server():
            logging.info('...waiting for server to be destroyed')
            time.sleep(1)
        logging.info('...done.')
    else:
        logging.error('No environment exists.')
Beispiel #28
0
def envy_ssh(args):
    """SSH into the current server."""
    config = _get_config(args)

    # if user defines -n in cli, append name to project name.
    if args.name:
        config['project_config']['name'] = '%s-%s' % (
            config['project_config']['name'], args.name)

    envy = Envy(config)
    remote_user = config['project_config']['remote_user']
    if envy.ip():
        disable_known_hosts = ('-o UserKnownHostsFile=/dev/null'
                               ' -o StrictHostKeyChecking=no')
        fabric.operations.local('ssh %s %s@%s' % (disable_known_hosts,
                                                  remote_user,
                                                  envy.ip()))
    else:
        logging.error('Could not find IP.')
Beispiel #29
0
def envy_scp(args):
    """SCP Files to your ENVy"""
    config = _get_config(args)

    # if user defines -n in cli, append name to project name.
    if args.name:
        config['project_config']['name'] = '%s-%s' % (
            config['project_config']['name'], args.name)

    envy = Envy(config)

    if envy.ip():
        remote_user = '******'
        host_string = '%s@%s' % (remote_user, envy.ip())

        with fabric.api.settings(host_string=host_string):
            fabric.operations.put(args.source, args.target)
    else:
        logging.error('Could not find IP to upload file to.')
Beispiel #30
0
    def run(self, config, args):
        envy = Envy(config)

        if envy.ip():
            host_string = '%s@%s' % (envy.remote_user, envy.ip())

            with fabric.api.settings(host_string=host_string):
                file_list = [(os.path.expanduser(filename), location) for
                             filename, location in
                             envy.project_config.get('files', {}).iteritems()]

                for filename, endlocation in file_list:
                    logging.info("Putting file from '%s' to '%s'",
                                 filename, endlocation)

                    if os.path.exists(filename):
                        self._put_file(filename, endlocation)
                    else:
                        logging.warning("File '%s' not found.", filename)

        else:
            logging.error('Could not determine IP.')
Beispiel #31
0
def envy_ip(args):
    """Show the IP of the current server."""
    config = _get_config(args)

    # if user defines -n in cli, append name to project name.
    if args.name:
        config['project_config']['name'] = '%s-%s' % (
            config['project_config']['name'], args.name)

    envy = Envy(config)

    if not envy.server():
        logging.error('Environment has not been created.\n'
                      'Try running `envy up` first?')
    elif envy.ip():
        print envy.ip()
    else:
        logging.error('Could not find IP.')
Beispiel #32
0
def envy_destroy(args):
    """Power-off and destroy the current server."""
    config = _get_config(args)

    # if user defines -n in cli, append name to project name.
    if args.name:
        config['project_config']['name'] = '%s-%s' % (
            config['project_config']['name'], args.name)

    envy = Envy(config)
    logging.info('Triggering environment deletion.')
    if envy.find_server():
        envy.delete_server()
        while envy.find_server():
            logging.info('...waiting for server to be destroyed')
            time.sleep(1)
        logging.info('...done.')
    else:
        logging.error('No environment exists.')
Beispiel #33
0
 def run(self, config, args):
     envy = Envy(config)
     envy.snapshot('%s-snapshot' % envy.name)
Beispiel #34
0
    def run(self, config, args):
        envy = Envy(config)

        for server in envy.list_servers():
            if server.name.startswith(envy.name):
                print server.name