Beispiel #1
0
def run(config, argv):
    args = docopt(__doc__, argv=argv)

    active_env = config.environment
    env = args["<environment>"]
    if not env:
        local_path = os.path.join(Filesystem.local_path())
        dirs = (x for x in next(os.walk(local_path))[1] if x != "current")
        for d in dirs:
            if active_env == d:
                sys.stdout.write("* {0}\n".format(d))
            else:
                sys.stdout.write("  {0}\n".format(d))
        return

    env_path = Filesystem.local_env_path(env)

    if env == active_env:
        sys.stdout.write("Already on '{0}'\n".format(env))
    elif os.path.isdir(env_path):
        config.activate_environment(env)
        sys.stdout.write("Switched to environment '{0}'\n".format(env))
    else:
        sys.stdout.write("Environment '{0}' does not exist\n".format(env))
Beispiel #2
0
def run(config, argv):

    args = docopt(__doc__, argv=argv)



    # this is what makes up the above
    profile = config.profile['master']
    identity = None
    hostname = None
    username = None

    try:
        provider = config.provider_for_profile(profile)
    except UnknownConfigProvider as e:
        sys.stdout.write(
            'Unknown config provider \'{0}\', unable to continue.\n'\
            .format(e.message))
        return

    with profile_key_error():
        username = profile['ssh_username']

    with config_key_error():
        hostname = config.data['master']

    identity = profile.get('ssh_password', provider.ssh_identity())

    if not identity:
        log.error('No identity specificed.\n\
Please set ssh_password on the master profile or provide a private_key \
in your provider for this master')
        return

    current_env = config.environment

    if not current_env:
        sys.stdout.write('No environment available.\n')
        sys.stdout.write('Have you run \'cloudseed init env <environment>\'?\n')
        return

    sys.stdout.write('Syncing states for \'{0}\'\n'.format(current_env))

    env_path = Filesystem.local_env_path(current_env)
    states_path = os.path.join(env_path, 'states')

    if not os.path.isdir(states_path):
        sys.stdout.write('States dir not found at \'%s\'\n', states_path)
        return

    master_config = salt_master_config(config)
    sys.stdout.write('Archiving contents at \'{0}\'\n'.format(states_path))

    tmp = tempfile.NamedTemporaryFile(delete=False)

    archive = tarfile.open(fileobj=tmp, mode='w:gz')
    archive.add(states_path, '.')
    archive.close()

    tmp.close()
    log.debug('Archive created at %s', tmp.name)

    try:
        remote_path = master_config['file_roots']['base'][0]
    except KeyError:
        remote_path = '/srv/salt'

    remote_file = os.path.join('/tmp', os.path.basename(tmp.name))

    log.debug('Initializing SSH Client')
    with ssh_client_error():
        ssh_client = ssh.master_client_with_config(config)

    log.debug('Initializing SFTP Client')
    sftp_client = sftp.connect(
        hostname=hostname,
        username=username,
        identity=identity)

    log.debug(
        'Remotely Executing: sudo sh -c "mkdir -p %s; chown -R root:root %s"',
        remote_path, remote_path)

    ssh.run(
        ssh_client,
        'sudo sh -c "mkdir -p {0}; chown -R root:root {0}"'.format(remote_path))

    sys.stdout.write('Transferring archive to \'{0}\'\n'.format(hostname))
    sftp.put(sftp_client, tmp.name, remote_file)
    sys.stdout.write('Unpacking archive to \'{0}\'\n'.format(remote_path))

    log.debug(
        'Remotely Executing: sudo sh -c "tar -C %s -xvf %s; chwon -R root:root %s"',
        remote_path, remote_file, remote_path)

    ssh.run(ssh_client,
        'sudo sh -c "tar -C {0} -xvf {1}; chown -R root:root {0}"'\
        .format(remote_path, remote_file))

    log.debug(
        'Remotely Executing: rm -rf %s',
        remote_file)

    ssh.run(ssh_client,
        'rm -f {0}'\
        .format(remote_file))

    os.unlink(tmp.name)

    # debugging command only
    ssh.run(ssh_client,
            'sudo sh -c "salt \'master\' state.highstate --async"')

    sys.stdout.write('Sync complete\n')
def init_cloudseed_environment(config, args):
    env_name = args['<environment>']
    # project_name = config.data['project']

    write_file = Filesystem.write_file

    env_dir = Filesystem.local_env_path(env_name)
    states_dir = os.path.join(env_dir, 'states')
    profile_path = os.path.join(env_dir, 'profile')
    master_path = os.path.join(env_dir, 'master')
    config_path = os.path.join(env_dir, 'config')
    # project_env_dir = Filesystem.project_env_path(project_name, env_name)

    profile = {
        'master': {
            'image': '<server image>',
            'size': '<server size>',
            'script': '<bootstrap script>',
            'ssh_username': '******',
            'provider': '<provider_name>'
        },

        'minion': {
            'image': '<server image>',
            'size': '<server size>',
            'script': '<bootstrap script>',
            'ssh_username': '******',
            'provider': '<provider_name>'
        }
    }

    master = {
        'fileserver_backend': [
            'roots',
            'git'
        ],

        'gitfs_remotes': [
            'git://github.com/cloudseed-project/cloudseed-states.git'
        ],

        'file_roots': {
            'base': ['/srv/salt']
        }
    }

    if not os.path.isdir(env_dir):
        log.debug('Creating directory %s', env_dir)
        os.mkdir(env_dir)

    if not os.path.isdir(states_dir):
        log.debug('Creating directory %s', states_dir)
        os.mkdir(states_dir)

    if os.path.exists(profile_path):
        log.debug('%s already exists, will not overwrite', profile_path)
    else:
        write_file(profile_path, profile)

    if os.path.exists(config_path):
        log.debug('%s already exists, will not overwrite', config_path)
    else:
        open(config_path, 'w').close()

    if os.path.exists(master_path):
        log.debug('%s already exists, will not overwrite', master_path)
    else:
        write_file(master_path, master)