def list_inner_scopes(root_dir, version):
with cd('{}/{}'.format(root_dir, version)), hide('running', 'stdout'):
result = run('''find ./ -mindepth 1 -maxdepth 1 -type d -print''')
return [each.lstrip('./') for each in result.splitlines()]
python类hide()的实例源码
def list_existed_versions(root_dir):
"""
???????????????
"""
with cd(root_dir), hide('running', 'stdout'):
result = run('''( find ./ -mindepth 1 -maxdepth 1 -type d -print |grep --color=never -E '[0-9]+(\.[0-9]+){3}\\b' ) || echo "no_version_found"''')
if result == "no_version_found":
return []
else:
return [each.lstrip('./') for each in result.splitlines()]
def rsync_to_backup(game, region):
print("??????????????...")
sys.stdout.flush()
time.sleep(30)
config = ConfigReader(game, region)
rsync_module = config.get("rsync_module")
rsync_root = config.get("rsync_root")
rsync_backup_ip = config.get("rsync_backup_ip")
if rsync_module == "" or rsync_root == "" or rsync_backup_ip == "" :
raise Exception('rsync config is not proper in the game config file')
with cd(rsync_root), settings(user='root'), hide("stdout"):
run('''rsync -art -R --delete --out-format="%n" --password-file=/etc/rsyncd.secret ./ {}::{}'''.format(rsync_backup_ip, rsync_module))
#for i in range(3):
# with cd(rsync_root), settings(user='root'), hide("stdout"):
# out = run('''rsync -art -R --dry-run --delete --out-format="%n" --password-file=/etc/rsyncd.secret ./ {}::{}'''.format(rsync_backup_ip, rsync_module), timeout=120)
#
# if out.strip() != "":
# print("??????????????, ??60s?????...")
# sys.stdout.flush()
# time.sleep(60)
# else:
# print("??????!")
# break
#else:
# print("[WARNING]: ?????????, 30s??????version.lua, ????????!!!!!!!")
# sys.stdout.flush()
# time.sleep(30)
def exec_sql(game, language, backstage_db, backstageip, sql ):
with settings(hide('running', 'stdout', 'stderr'),host_string=backstageip):
return run('pandora -e "use {};{}"'.format(backstage_db,sql ))
def clean(name, ask=True, **kwargs):
"""
Remove an experiment.
:param name: experiment name (or absolute path to experiment)
:param ask: ask confirmation
:param kwargs: simulation keyword arguments (see the documentation for more information)
"""
path = kwargs.get('path')
console = kwargs.get('console')
if console is None or not any([i['name'] == name and i['status'] == 'PENDING' for i in console.tasklist.values()]):
logger.debug(" > Cleaning folder...")
with hide(*HIDDEN_ALL):
local("rm -rf {}".format(path))
return "Cleaned"
def cooja(name, with_malicious=True, **kwargs):
"""
Start an experiment in Cooja with/without the malicious mote and updates the experiment if motes' positions
were changed.
:param name: experiment name
:param with_malicious: use the simulation WITH the malicious mote or not
:param path: expanded path of the experiment (dynamically filled in through 'command' decorator with 'expand')
:param kwargs: simulation keyword arguments (see the documentation for more information)
"""
sim_path = join(kwargs['path'], 'with{}-malicious'.format(['out', ''][with_malicious is True]))
motes_before = get_motes_from_simulation(join(sim_path, 'simulation.csc'), as_dictionary=True)
with hide(*HIDDEN_ALL):
with lcd(sim_path):
local("make cooja TASK={}".format(kwargs.get('task', "cooja")))
motes_after = get_motes_from_simulation(join(sim_path, 'simulation.csc'), as_dictionary=True)
# if there was a change, update the other simulation in this experiment
if len(set(motes_before.items()) & set(motes_after.items())) > 0:
other_sim_path = join(kwargs['path'], 'with{}-malicious'.format(['', 'out'][with_malicious is True]))
set_motes_to_simulation(join(other_sim_path, 'simulation.csc'), motes_after)
# if this experiment is part of a campaign, update this
campaign = read_config(kwargs['path']).get('campaign')
if campaign is not None:
for experiment in get_experiments(campaign):
if experiment in ['BASE', name]:
continue
exp_path = join(EXPERIMENT_FOLDER, experiment)
set_motes_to_simulation(join(exp_path, 'with-malicious', 'simulation.csc'), motes_after)
set_motes_to_simulation(join(exp_path, 'without-malicious', 'simulation.csc'), motes_after)
def update(silent=False, **kwargs):
"""
Update Contiki-OS and RPL Attacks Framework.
:param silent: run command silently
:param kwargs: simulation keyword arguments (see the documentation for more information)
"""
for folder, repository in zip([CONTIKI_FOLDER, FRAMEWORK_FOLDER], ["Contiki-OS", "RPL Attacks Framework"]):
with hide(*HIDDEN_ALL):
with lcd(folder):
if "Could not resolve proxy" in local('git fetch --all', capture=True):
logger.error("Update failed ; please check your proxy settings")
break
uptodate = "branch is up-to-date" in local('git checkout master', capture=True).strip().split('\n')[-1]
if not uptodate:
req_exists = exists("requirements.txt")
if req_exists:
req_md5 = hash_file("requirements.txt")
logger.warn("You are about to loose any custom change made to {} ;".format(repository))
if silent or std_input("Proceed anyway ? (yes|no) [default: no] ", 'yellow') == 'yes':
local('git submodule update --init')
local('git fetch --all')
local('git reset --hard origin/master')
local('git pull')
if req_exists and hash_file("requirements.txt") != req_md5:
local('pip install -r requirements.txt')
if repository == "RPL Attacks Framework":
remove_files(folder, "Vagrantfile")
remove_folder(join(folder, "provisioning"))
logger.debug(" > {} {}".format(repository, ["updated", "already up-to-date"][uptodate]))
if not silent:
logger.warn("Restarting the framework...")
restart(PIDFILE)
def make_incremental_build(name, basis="live"):
"""
Archive a build, hard-linking unchanged files from the "basis" build (default live)
This can significantly reduce the disk space used by multiple builds.
On mac, requires ``brew install coreutils``
"""
cp_bin = find_executable('gcp')
if find_executable('gcp'):
cp_bin = "gcp"
else:
cp_bin = "cp"
live_name = dealias_build("_live")
stage_name = dealias_build("_stage")
if name in (live_name, stage_name):
abort("Cannot turn the live or stage build into an incremental build")
basis = dealias_build(basis)
stop(name)
with lcd("%s/deploy/builds" % ROOT_DIR):
local("mv %(name)s %(name)s~" % {'name': name})
with settings(hide("stderr"), warn_only=True):
cp_ret = local("%(cp_bin)s -al %(basis)s %(name)s" % {
'basis': basis,
'name': name,
'cp_bin': cp_bin,
})
if not cp_ret.succeeded:
local("mv %(name)s~ %(name)s" % {'name': name})
abort("Local cp bin does not support -l flag (on mac: brew install coreutils)")
local("rsync -acH --delete %(name)s~/ %(name)s" % {'name': name})
local("rm -rf %(name)s~" % {'name': name})
def exist(self):
with settings(warn_only=True):
with hide('commands'):
result = local('systemctl list-unit-files', capture=True)
for line in result.split('\n'):
if line.startswith(self.name):
return True
return False
def install_mysql():
with settings(hide('warnings', 'stderr'), warn_only=True):
result = sudo('dpkg-query --show mysql-server')
if result.failed is False:
warn('MySQL is already installed')
return
mysql_password = prompt('Please enter MySQL root password:')
sudo('echo "mysql-server-5.5 mysql-server/root_password password ' \
'%s" | debconf-set-selections' % mysql_password)
sudo('echo "mysql-server-5.5 mysql-server/root_password_again password ' \
'%s" | debconf-set-selections' % mysql_password)
apt_get_install('mysql-server')
def spot_price_history(inst_type=INSTANCE_TYPE, region='us-west-2'):
' spot price history '
now = datetime.datetime.utcnow()
start_time = '{:%Y-%m-%dT%H:00:00}'.format(now)
aws_cmd = 'aws ec2 describe-spot-price-history --start-time {} --product "Linux/UNIX" --instance-type "{}"'
aws_cmd = aws_cmd.format(start_time, inst_type)
jq_cmd = 'jq -c -C ".SpotPriceHistory[] | {SpotPrice, AvailabilityZone, InstanceType }"'
with hide("running"):
os.environ['AWS_DEFAULT_REGION'] = region
local('|'.join([aws_cmd, jq_cmd]))
def regions():
' get list of regions '
aws_cmd = 'aws ec2 describe-regions'
jq_cmd = 'jq -c -C ".Regions[]"'
with hide("running"):
local('|'.join([aws_cmd, jq_cmd]))
def run_instances():
' run instances '
aws_cmd = 'aws ec2 run-instances --image-id ami-9abea4fb --count 1 --instance-type m3.medium --key-name ubuntu_trusty --security-group-ids ssh-ip-only'
jq_cmd = 'jq -c -C "."'
with hide("running"):
local('|'.join([aws_cmd, jq_cmd]))
def describe_instances():
' describe instances '
aws_cmd = 'aws ec2 describe-instances'
jq_cmd = 'jq -c -C ".Reservations[].Instances[]| {InstanceType, KeyName, State: .State.Name, PublicIpAddress, InstanceId, ImageId}"'
with hide("running"):
local('|'.join([aws_cmd, jq_cmd]))
def cancel_spot_instance_requests(request_id):
' cancel spot instance requests '
aws_cmd = 'aws ec2 cancel-spot-instance-requests --spot-instance-request-ids {}'.format(request_id)
jq_cmd = 'jq -c -C ".CancelledSpotInstanceRequests[] | {State, RequestId:.SpotInstanceRequestId}"'
with hide("running"):
local('|'.join([aws_cmd, jq_cmd]))
# local(aws_cmd)
def terminate_instances(inst_id):
' terminate instances '
aws_cmd = 'aws ec2 terminate-instances --instance-ids {}'
aws_cmd = aws_cmd.format(inst_id)
with hide("running"):
local(aws_cmd)
def storm_list():
' list ssh config '
cmd = 'storm list'
with hide("running"):
local(cmd)
def storm_delete(name):
' delete ssh config by name '
cmd = 'storm delete {}'.format(name)
with hide("running"):
local(cmd)
def install_mongodb(*args, **kwargs):
version = kwargs.get('version') or '3.2.6'
home = run('echo $HOME')
port = kwargs.get('port')
base_dir = kwargs.get('base_dir') or os.path.join(home, 'mongodb')
data_dir = os.path.join(base_dir, 'data')
data_backup_dir = os.path.join(home, 'mongodb_data_bak')
if not port:
print('Enter a local port for the mongodb server')
return 1
if exists(data_dir):
if exists(data_backup_dir):
print('Backup directory %s already exists.' % data_backup_dir)
return 1
else:
run('mv %s %s' % (data_dir, data_backup_dir))
if exists(base_dir):
run('rm -rf %s' % (base_dir))
LONG_BIT = run('getconf LONG_BIT')
if LONG_BIT == '64':
package_name = 'mongodb-linux-x86_64-%s.tgz' % version
else:
package_name = 'mongodb-linux-i686-%s.tgz' % version
package_path = os.path.join(home, package_name)
if not exists(package_path):
with hide('output'):
run('wget https://fastdl.mongodb.org/linux/%s -O %s' % (package_name, package_path))
if not exists(package_path.replace('.tgz', '')):
run('tar xzf ' + package_path)
run('mv %s %s' % (package_path.replace('.tgz', ''), base_dir))
if exists(data_backup_dir):
run('mv %s %s' % (data_backup_dir, data_dir))
else:
run('mkdir -p %s/db' % data_dir)
base_conf = [
'port = %s' % port,
]
spec_conf = [
'bind_ip = 127.0.0.1',
'logappend = True',
'journal = true',
'nohttpinterface = true',
]
load_config(
os.path.join(base_dir, 'mongodb.conf'),
base_conf=base_conf, spec_conf=spec_conf, delimiter='= '
)
if not exists('~/init'):
run('mkdir ~/init')
run('wget https://templates.wservices.ch/mongodb/init.%s -O ~/init/mongodb' % LONG_BIT)
sed('~/init/mongodb', 'DAEMON=/usr/bin/mongod', 'DAEMON=$HOME/mongodb/bin/mongod')
run('chmod 750 ~/init/mongodb')
run('~/init/mongodb start')