def file(dest, mode='644', owner='root:root', src=None, src_file=None, src_str=None, override=False): if src_str is not None: src_file = __create_src_file(dest, src_str) is_updated = False with api.warn_only(): if exists(dest) and not override: log.info('file "{0}" exists'.format(dest)) else: if not src_file: src_file = __get_src_file(dest, src_dirname='files', src=src) if env.is_local: sudo('cp {0} {1}'.format(src_file, dest)) else: scp(src_file, dest) is_updated = True sudo('chmod -R {0} {1}'.format(mode, dest) + ' && chown -R {0} {1}'.format(owner, dest)) return is_updated
def run_remote(): remote = env.remote_map[env.host] root, dirname = CONF._repo_dir.rsplit('/', 1) storage_dir = os.path.join(dirname, CONF._storage_dir.rsplit('/', 1)[1]) repo_tar = '{0}/fabrepo.tar.gz'.format(CONF._tmp_dir) remote_repo = '{0}/fabrepo'.format(CONF._remote_tmp_dir) remote_repo_tar = '{0}/fabrepo.tar.gz'.format(CONF._remote_tmp_dir) local('cd {0} && tar -zcf {1} {2} --exclude .git --exclude {3}'.format( root, repo_tar, dirname, storage_dir)) scp(repo_tar, remote_repo_tar) run('rm -rf $HOME/fabric-repo') run('cd {0} && tar -xf fabrepo.tar.gz -C $HOME'.format(CONF._remote_tmp_dir, remote_repo)) task_name = '' if env.is_setup: task_name = 'setup' elif env.is_check: task_name = 'check' elif env.is_manage: task_name = 'manage:{0}'.format(','.join(env.func_names)) cluster_map = {} with api.shell_env(password=env.password): for cluster in remote['clusters']: run('cd $HOME/{0} && fab node:{1}/{2},yes {3} -u $USER -p $password'.format( dirname, cluster, remote['host_pattern'], task_name, env.password)) with api.warn_only(): yaml_str = run('cat $HOME/{0}/nodes/{1}/__cluster.yml'.format(dirname, cluster)) cluster_map[cluster] = yaml.load(yaml_str) return cluster_map
def template(dest, mode='644', owner='root:root', data={}, src=None, src_file=None, src_str=None, insert_eol_crlf=True): template_data = {} template_data.update(data) template_data['node'] = env.node template_data['cluster'] = env.cluster is_updated = False if src_str: src_file = __create_src_file(dest, src_str) if not src_file: src_file = __get_src_file(dest, src_dirname='templates', src=src) timestamp = int(time.time()) tmp_path = 'templates/{0}_{1}'.format(dest, timestamp) tmp_path = os.path.join(CONF._remote_storage_dir, tmp_path) # local_tmp_file = os.path.join(conf.TMP_DIR, env.host, tmp_path) local_tmp_file = CONF._tmp_dir + '/' + env.host + '/' + tmp_path local_tmp_dir = local_tmp_file.rsplit('/', 1)[0] mkdir(local_tmp_dir, is_local=True, use_sudo=False) template = j2_env.get_template(src_file) if not env.is_test: with open(local_tmp_file, 'w') as exf: exf.write(template.render(**template_data).encode('utf-8')) if insert_eol_crlf: exf.write('\n') if env.is_local: with api.warn_only(): if exists(dest): result = sudo('diff {0} {1}'.format(dest, local_tmp_file)) if result.return_code != 0: sudo('mv {0} {1}_old'.format(dest, local_tmp_file)) sudo('cp -f {0} {1}'.format(local_tmp_file, dest)) is_updated = True else: log.info('No change') else: sudo('diff /dev/null {1}'.format(dest, local_tmp_file)) sudo('cp -f {0} {1}'.format(local_tmp_file, dest)) is_updated = True else: tmp_dir = tmp_path.rsplit('/', 1)[0] mkdir(tmp_dir, mode='770', owner='{0}:root'.format(env.user)) scp(local_tmp_file, tmp_path) with api.warn_only(): if exists(dest): result = sudo('diff {0} {1}'.format(dest, tmp_path)) if result.return_code != 0: sudo('mv {0} {1}_old'.format(dest, tmp_path)) sudo('cp -f {0} {1}'.format(tmp_path, dest)) is_updated = True else: log.info('No change') else: sudo('diff /dev/null {1}'.format(dest, tmp_path)) sudo('cp -f {0} {1}'.format(tmp_path, dest)) is_updated = True sudo('sh -c "chmod {0} {1}'.format(mode, dest) + ' && chown {0} {1}"'.format(owner, dest)) return is_updated
def setup(self): data = self.init() var_dir = CONF.client.package_var_dir tmp_dir = os.path.join(var_dir, 'tmp') log_dir = '/var/log/fabkit' common_repo = '{0}/fabkit-repo-common'.format(var_dir) client_repo = '{0}/fabkit-repo-client'.format(var_dir) server_repo = '{0}/fabkit-repo-server'.format(var_dir) if data['use_package']: Package(data['package_name']).install() return self.install_packages() self.python.setup() filer.mkdir(var_dir, owner=data['owner']) filer.mkdir(log_dir, owner=data['owner']) filer.mkdir(tmp_dir, owner=data['owner'], mode='777') filer.mkdir(common_repo, owner=data['owner']) filer.mkdir(client_repo, owner=data['owner']) filer.mkdir(server_repo, owner=data['owner']) sudo('rm -rf {0}/fabfile*'.format(tmp_dir)) fabfile_tar_gz = os.path.join(tmp_dir, 'fabfile.tar.gz') scp('/tmp/fabfile.tar.gz', fabfile_tar_gz) sudo('rm -rf {0}/fabfile'.format(common_repo)) sudo('cd {0} && tar xzf {1} && ' 'cp -r fabfile {2}/fabfile'.format( tmp_dir, fabfile_tar_gz, common_repo)) sudo('{0}/bin/pip install -r {1}/fabfile/requirements.txt'.format( CONF.client.package_prefix, common_repo)) data['repo'] = client_repo filer.template('{0}/bin/fabclient'.format(CONF.client.package_prefix), src='fabric.sh', data=data, mode='755') data['repo'] = server_repo filer.template('{0}/bin/fabserver'.format(CONF.client.package_prefix), src='fabric.sh', data=data, mode='755') filer.template('{0}/bin/fabnode'.format(CONF.client.package_prefix), src='fabnode.sh', data=data, mode='755') filer.template('/etc/systemd/system/fabagent.service', src='systemd.service', data={ 'description': 'fabagent', 'exec': '/opt/fabkit/bin/fabclient agent', 'user': '******', }) filer.template('/etc/systemd/system/fabagent-central.service', src='systemd.service', data={ 'description': 'fabagent', 'exec': '/opt/fabkit/bin/fabclient agent_central', 'user': '******', }) filer.template('/etc/systemd/system/fabnode.service', src='systemd.service', data={ 'description': 'fabnode', 'exec': '/opt/fabkit/bin/fabnode', # noqa 'user': '******', }) sudo('systemctl daemon-reload') sudo('npm install -g coffee-script') sudo('cd {0}/fabfile/core/webapp/fabnode && ' 'npm install'.format(common_repo))
def setup(self): data = self.init() var_dir = CONF.client.package_var_dir tmp_dir = os.path.join(var_dir, 'tmp') log_dir = '/var/log/fabkit' common_repo = '{0}/fabkit-repo-common'.format(var_dir) client_repo = '{0}/fabkit-repo-client'.format(var_dir) server_repo = '{0}/fabkit-repo-server'.format(var_dir) if data['use_package']: Package(data['package_name']).install() return self.install_packages() self.python.setup() filer.mkdir(var_dir, owner=data['owner']) filer.mkdir(log_dir, owner=data['owner']) filer.mkdir(tmp_dir, owner=data['owner'], mode='777') filer.mkdir(common_repo, owner=data['owner']) filer.mkdir(client_repo, owner=data['owner']) filer.mkdir(server_repo, owner=data['owner']) sudo('rm -rf {0}/fabfile*'.format(tmp_dir)) fabfile_tar_gz = os.path.join(tmp_dir, 'fabfile.tar.gz') scp('/tmp/fabfile.tar.gz', fabfile_tar_gz) sudo('rm -rf {0}/fabfile'.format(common_repo)) sudo('cd {0} && tar xzf {1} && ' 'cp -r fabfile {2}/fabfile'.format(tmp_dir, fabfile_tar_gz, common_repo)) sudo('{0}/bin/pip install -r {1}/fabfile/requirements.txt'.format( CONF.client.package_prefix, common_repo)) data['repo'] = client_repo filer.template('{0}/bin/fabclient'.format(CONF.client.package_prefix), src='fabric.sh', data=data, mode='755') data['repo'] = server_repo filer.template('{0}/bin/fabserver'.format(CONF.client.package_prefix), src='fabric.sh', data=data, mode='755') filer.template('{0}/bin/fabnode'.format(CONF.client.package_prefix), src='fabnode.sh', data=data, mode='755') filer.template('/etc/systemd/system/fabagent.service', src='systemd.service', data={ 'description': 'fabagent', 'exec': '/opt/fabkit/bin/fabclient agent', 'user': '******', }) filer.template('/etc/systemd/system/fabagent-central.service', src='systemd.service', data={ 'description': 'fabagent', 'exec': '/opt/fabkit/bin/fabclient agent_central', 'user': '******', }) filer.template( '/etc/systemd/system/fabnode.service', src='systemd.service', data={ 'description': 'fabnode', 'exec': '/opt/fabkit/bin/fabnode', # noqa 'user': '******', }) sudo('systemctl daemon-reload') sudo('npm install -g coffee-script') sudo('cd {0}/fabfile/core/webapp/fabnode && ' 'npm install'.format(common_repo))