-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfabfile.py
executable file
·248 lines (200 loc) · 7.96 KB
/
fabfile.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# from fabric import Connection
# from fabric import task
# c=Connection('tbxy09@localhost')
# from fabric.api import cd, lcd, env, local, parallel, serial
import logging
# logging.basicConfig(level=logging.DEBUG)
# from fabric.api import cd, lcd, env, local,serial
# from fabric.api import put, run, settings, sudo, prefix
# from fabric.operations import prompt
# from fabric import task,Connection
from fabric import task
from fabric.contrib import django
# from fabric.contrib import files
# from fabric.state import connections
# from fabric.colors import red, green, blue, cyan, magenta, white, yellow
from boto.s3.connection import S3Connection
from boto.s3.key import Key
from boto.ec2.connection import EC2Connection
from tencent_cloud_api import describeIns,updataIngress
# from vendor import yaml
from pprint import pprint
from collections import defaultdict
from contextlib import contextmanager as _contextmanager
# from fabric2 import Connection
import os
import time
import sys
import re
django.settings_module('settings')
try:
from django.conf import settings as django_settings
except ImportError:
print( " ---> Django not installed yet." )
django_settings = None
# ============
# = DEFAULTS =
# ============
# env.hosts = ['tbxy09@localhost']
# # env.SECRETS_PATH=
# env.SECRETS_PATH = '/srv/sowhat'
# env.app_directory='/opt/playground/sowhat'
# c=Connection('tbxy09@localhost')
# def setup_hosts():
# put(os.path.join(env.SECRETS_PATH, 'configs/hosts'), '/etc/hosts', use_sudo=True)
# sudo('echo "\n\n127.0.0.1 `hostname`" | sudo tee -a /etc/hosts')
# def copy_certificates():
# cert_path = '%s/config/certificates' % env.NEWSBLUR_PATH
# run('mkdir -p %s' % cert_path)
# put(os.path.join(env.SECRETS_PATH, 'certificates/newsblur.com.crt'), cert_path)
# put(os.path.join(env.SECRETS_PATH, 'certificates/newsblur.com.key'), cert_path)
# put(os.path.join(env.SECRETS_PATH, 'certificates/comodo/newsblur.com.pem'), cert_path)
# put(os.path.join(env.SECRETS_PATH, 'certificates/comodo/dhparams.pem'), cert_path)
# put(os.path.join(env.SECRETS_PATH, 'certificates/ios/aps_development.pem'), cert_path)
# put(os.path.join(env.SECRETS_PATH, 'certificates/ios/aps.pem'), cert_path)
# run('cat %s/newsblur.com.pem > %s/newsblur.pem' % (cert_path, cert_path))
# run('echo "\n" >> %s/newsblur.pem' % (cert_path))
# run('cat %s/newsblur.com.key >> %s/newsblur.pem' % (cert_path, cert_path))
def copy_ssh_keys(username='tbxy09', private=False):
# sudo('mkdir -p ~%s/.ssh' % username)
put(os.path.join(env.SECRETS_PATH, 'keys/newsblur.key.pub'), 'local.key.pub')
sudo('mv local.key.pub ~%s/.ssh/id_rsa.pub' % username)
if private:
put(os.path.join(env.SECRETS_PATH, 'keys/newsblur.key'), 'local.key')
sudo('mv local.key ~%s/.ssh/id_rsa' % username)
sudo("echo \"\n\" >> ~%s/.ssh/authorized_keys" % username)
sudo("echo `cat ~%s/.ssh/id_rsa.pub` >> ~%s/.ssh/authorized_keys" % (username, username))
sudo('chown -R %s.%s ~%s/.ssh' % (username, username, username))
sudo('chmod 700 ~%s/.ssh' % username)
sudo('chmod 600 ~%s/.ssh/id_rsa*' % username)
def setup_launch_git():
# with cd('/opt/playground/sowhat'):
with cd(env.app_directory):
run('./setup_sowhat.sh')
@task
def setup_regular_task(c):
# print('running the setup_regular_task')
# run('cd /opt/playground/sowhat')
# with cd('/opt/playground/sowhat'):
with cd(env.app_directory):
c.run('cp /root/vimwiki/diary/Makefile Makefile')
# def get_latest_commit():
# return urlopen(env.commits_server).read()
# @task
def deploy():
with cd(env.app_directory):
pass
# # @parallel
# def setup_hosts():
# put(os.path.join(env.SECRETS_PATH, 'configs/hosts'), '/etc/hosts', use_sudo=True)
# sudo('echo "\n\n127.0.0.1 `hostname`" | sudo tee -a /etc/hosts')
# @task
def get_ip():
run('curl ipinfo.io/ip')
# run('curl www.baidu.com; bin/sleep 1')
# put(os.path.join(env.SECRETS_PATH, 'configs/hosts'), '/etc/hosts', use_sudo=True)
# sudo('echo "\n\n127.0.0.1 `hostname`" | sudo tee -a /etc/hosts')
# @task
def des_aliyunIns(c):
from aliyun_cloud_api import describeIns,describeSec,modifySec
cred=[os.getenv('ALIYUN_SECRET_ID'),
os.getenv('ALIYUN_SECRET_KEY')]
print(cred)
# instance=describeIns(*cred)
describeSec(*cred)
# modifySec(*cred)
# @task
def des_tcIns(c):
from tencentcloud.common import credential
cred = credential.Credential(
django_settings.TC_SECRET_ID,
django_settings.TC_SECRET_KEY
# os.getenv('TENCENTCLOUD_SECRET_ID'),
# os.getenv('TENCENTCLOUD_SECRET_KEY')
)
# return a instance
describeIns(cred)
# instance = updataIngress(cred)
# adding to the host list,this is the ip address
# host = instance.public_dns_name
# env.host_string = host
def setup_ec2():
AMI_NAME = 'ami-834cf1ea' # Ubuntu 64-bit 12.04 LTS
# INSTANCE_TYPE = 'c1.medium'
INSTANCE_TYPE = 'c1.medium'
conn = EC2Connection(django_settings.AWS_ACCESS_KEY_ID, django_settings.AWS_SECRET_ACCESS_KEY)
reservation = conn.run_instances(AMI_NAME, instance_type=INSTANCE_TYPE,
key_name=env.user,
security_groups=['db-mongo'])
instance = reservation.instances[0]
print( "Booting reservation: %s/%s (size: %s)" .format(reservation, instance, INSTANCE_TYPE) )
i = 0
while True:
if instance.state == 'pending':
print(".")
sys.stdout.flush()
instance.update()
i += 1
time.sleep(i)
elif instance.state == 'running':
print ("...booted: %s" .format(instance.public_dns_name))
time.sleep(5)
break
else:
print ("!!! Error: %s" .format(instance.state))
return
host = instance.public_dns_name
env.host_string = host
# ===========
# = Tencent =
# ===========
TENCENTCLOUD_SECRET_ID = os.getenv("TENCENTCLOUD_SECRET_ID")
TENCENTCLOUD_SECRET_KEY = os.getenv("TENCENTCLOUD_SECRET_KEY")
# ======
# = S3 =
# ======
if django_settings:
try:
ACCESS_KEY = django_settings.TC_SECRET_KEY
SECRET = django_settings.TC_SECRET_KEY
SECRET = django_settings.TC_SECRET_ID
# BUCKET_NAME = django_settings.S3_BACKUP_BUCKET # Note that you need to create this bucket first
except:
print( " ---> You need to fix django's settings. Enter python and type `import settings`." )
def save_file_in_s3(filename):
conn = S3Connection(ACCESS_KEY, SECRET)
bucket = conn.get_bucket(BUCKET_NAME)
k = Key(bucket)
k.key = filename
k.set_contents_from_filename(filename)
def get_file_from_s3(filename):
conn = S3Connection(ACCESS_KEY, SECRET)
bucket = conn.get_bucket(BUCKET_NAME)
k = Key(bucket)
k.key = filename
k.get_contents_to_filename(filename)
def list_backup_in_s3():
conn = S3Connection(ACCESS_KEY, SECRET)
bucket = conn.get_bucket(BUCKET_NAME)
for i, key in enumerate(bucket.get_all_keys()):
print ( "[%s] %s" .format(i, key.name) )
def delete_all_backups():
#FIXME: validate filename exists
conn = S3Connection(ACCESS_KEY, SECRET)
bucket = conn.get_bucket(BUCKET_NAME)
for i, key in enumerate(bucket.get_all_keys()):
print("deleting %s" .format(key.name))
key.delete()
# @task
def custom_runner(command):
"""Runs command with custom key_filename"""
with settings(warn_only=True, key_filename=r"~\.ssh\id_rsa.pub"):
result = run(command, timeout=5)
return result
@task
def xy(command):
"""Runs command with custom key_filename"""
with settings(warn_only=True, key_filename=r"~\.ssh\id_rsa.pub"):
result = run(command, timeout=5)
return result
# result = execute(custom_runner,"uname -a",host="tbxy09@localhost")