def master_client_with_config(config): profile = config.profile['master'] identity = None hostname = None username = None # raises UnknownConfigProvider provider = config.provider_for_profile(profile) # raises KeyError aka MissingProfileKey with profile_key_error(): username = profile['ssh_username'] log.debug('SSH Client Username: %s', username) # raises KeyError aka MissingConifgKey with config_key_error(): hostname = config.data['master'] log.debug('SSH Client Hostname: %s', hostname) identity = profile.get('ssh_password', provider.ssh_identity()) log.debug('SSH Client Identity: %s', identity) if not identity: raise MissingIdentity # 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 return connect( hostname=hostname, username=username, identity=identity)
def _create_instance(self, instance_name, profile, security_groups, user_data): with profile_key_error(): kwargs = { 'image_id': profile['image'], 'key_name': self.provider['keyname'], 'instance_type': profile['size'], 'security_groups': security_groups, 'user_data': user_data } self.log.debug('Creating instance with %s', kwargs) reservation = self.conn.run_instances(**kwargs) instance = reservation.instances[0] self.log.debug('Waiting for instance to become available, this can take a minute or so.') while True: if instance.public_dns_name: break; try: instance.update() except EC2ResponseError: pass time.sleep(3) self.log.debug('Naming instance %s', instance_name) self.log.debug('Instance available at %s', instance.public_dns_name) instance.add_tag('Name', instance_name) return instance.public_dns_name.encode('utf-8')
def connect_master(config): profile = config.profile['master'] # raises UnknownConfigProvider provider = config.provider_for_profile(profile) with profile_key_error(): username = profile['ssh_username'] with config_key_error(): hostname = config.data['master'] identity = provider.ssh_identity() log.debug('Opening SSH to %s@%s using identity %s', username, hostname, identity) call('ssh {0}@{1} -i {2} -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o IdentitiesOnly=yes' \ .format( username, hostname, identity), shell=True)
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')