1 def nginx():
2 resultBean = dict()
3 __salt__['cp.get_file']("salt:_shell/app/nginx.sh /usr/local/src/installnginx.sh")
4 jid = __salt__['cmd.async']
5 cmd = "sh /usr/local/src/installnginx.sh"
6 status, output = commands.getstatusoutput(cmd)
7 if status == 0:
8 resultBean['code'] = 0
9 resultBean['message'] = 'success'
10 resultBean['data'] = output
11 else:
12 resultBean['code'] = -1
13 resultBean['message'] = 'install nginx error'
14 resultBean['data'] = output
15 return resultBean
16
17 def tomcat():
18 pass
19
20 def keepalived():
21 pass
22
23 def lvs():
24 pass
25
26 def jdk():
27 pass
28
29 def mysql():
30 pass
31
32 def zookeeper():
33 pass
34
35 def redis():
36 pass
1 import codecs
2 import json
3 import commands
4
5
6 def publicKey(ipaddr):
7 with codecs.open('/tmp/{0}.txt'.format(ipaddr), 'w') as f:
8 f.write("{0} ansible_ssh_pass=123456".format(ipaddr))
9 resultBean = dict()
10 cmd = "ansible -i /tmp/{0}.txt all -m script -a '/srv/salt/_shell/installpub.sh'".format(ipaddr)
11 status, output = commands.getstatusoutput(cmd)
12 if status == 0:
13 resultBean['code'] = 0
14 resultBean['message'] = 'success'
15 resultBean['data'] = output
16 return json.dumps(resultBean)
1 import json
2
3 import requests
4
5
6 class SaltServer(object):
7 def __init__(self):
8 self.session = requests.session()
9 self.token = self.getToken()
10
11
12 def getToken(self):
13 url = "http://192.168.48.135:8000/login"
14 headers = {"Accept": "application/json"}
15 data = {
16 "username": "saltapi",
17 "password": "saltapi",
18 "eauth": "pam"
19 }
20 res = self.session.post(url=url, headers=headers, data=data)
21 text = res.text
22 result = json.loads(text)
23 token = result.get("return")[0].get("token")
24 return token
25
26 def runModules(self, minionid, fun, arg=None):
27 url = "http://192.168.48.135:8000"
28 data = {
29 "client": "local",
30 "tgt": minionid,
31 "fun": fun,
32 "arg": arg
33 }
34 resultBean = dict()
35 try:
36 res = self.session.post(url=url, data=data)
37 text = res.text
38 data = json.loads(text).get("return")
39 resultBean['code'] = 0
40 resultBean['message'] = "success"
41 resultBean['data'] = data
42 except Exception as e:
43 resultBean['code'] = 0
44 resultBean['message'] = "success"
45 resultBean['data'] = e
46 finally:
47 return resultBean
48
49
50 def runRunner(self, fun, **kwargs):
51 url = "http://192.168.48.135:8000"
52 data = {
53 "client": "runner",
54 "fun": fun,
55 }
56 data.update(kwargs)
57 resultBean = dict()
58 try:
59 res = self.session.post(url=url, data=data)
60 text = res.text
61 data = json.loads(text).get("return")
62 resultBean['code'] = 0
63 resultBean['message'] = "success"
64 resultBean['data'] = data
65 except Exception as e:
66 resultBean['code'] = 0
67 resultBean['message'] = "success"
68 resultBean['data'] = e
69 finally:
70 return resultBean
1 from lesson31.util.saltapi import SaltServer
2
3 saltServer = SaltServer()
4
5 # result1 = saltServer.runModules('192.168.48.129', 'cp.get_file', ['salt://_shell/app/nginx.sh', '/usr/local/src/installnginx.sh'])
6 # print(result1)
7 # result2 = saltServer.runModules('192.168.48.129', 'installApp.nginx')
8 # print(result2)
9 # result2 = saltServer.runRunner('manage.status')
10 # print(result2)
11 #
12 result1 = saltServer.runRunner('masterApp.publicKey', ipaddr='192.168.48.133')
13 print(result1)