Skip to content

Commit 6da4a70

Browse files
committed
change SANGFOROPER
1 parent f89d213 commit 6da4a70

1 file changed

Lines changed: 81 additions & 0 deletions

File tree

SANGFOROPER/Ecs_apireboot.py

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
#!/bin/env python3
2+
# -*- coding:UTF-8 -*-
3+
# _author:kaliarch
4+
from aliyunsdkcore import client
5+
from aliyunsdkecs.request.v20140526 import RebootInstanceRequest,StartInstanceRequest,StopInstanceRequest
6+
import time
7+
import os
8+
import logging
9+
class ecsOper():
10+
def __init__(self,logger):
11+
self.clentoper = client.AcsClient('<accessKeyId>', '<accessSecret>', 'cn-hangzhou')
12+
self.logger = logger
13+
self.logger.info("------------------------start reboot vpn ecs of API log-------------")
14+
def reboot_instance(self):
15+
# 设置参数
16+
request = RebootInstanceRequest.RebootInstanceRequest()
17+
request.set_accept_format('json')
18+
request.add_query_param('InstanceId', 'i-bjk23j1rlvfghlq79au')
19+
# 发起请求
20+
response = self.clentoper.do_action_with_exception(request)
21+
self.logger.info("public ecs vpn reboot successful!")
22+
self.logger.info(response)
23+
print(response)
24+
25+
def start_instance(self):
26+
request = StartInstanceRequest.StartInstanceRequest()
27+
request.set_accept_format('json')
28+
request.add_query_param('InstanceId', 'i-bjk23j1rlvfghlq79au')
29+
# 发起请求
30+
response = self.clentoper.do_action_with_exception(request)
31+
self.logger.info("public ecs vpn start successful!")
32+
self.logger.info(response)
33+
print(response)
34+
35+
def stop_instance(self):
36+
request = StopInstanceRequest.StopInstanceRequest()
37+
request.set_accept_format('json')
38+
request.add_query_param('InstanceId', 'i-bjk23j1rlvfghlq79au')
39+
request.add_query_param('ForceStop', 'false')
40+
# 发起请求
41+
response = self.clentoper.do_action_with_exception(request)
42+
self.logger.info(response)
43+
print(response)
44+
45+
def testlog(self):
46+
self.logger.info("public test log")
47+
48+
class Glp_Log:
49+
def __init__(self,filename):
50+
self.filename = filename
51+
def createDir(self):
52+
_LOGDIR = os.path.join(os.path.dirname(__file__), 'publiclog')
53+
print(_LOGDIR)
54+
_TIME = time.strftime('%Y-%m-%d', time.gmtime()) + '-'
55+
_LOGNAME = _TIME + self.filename
56+
print(_LOGNAME)
57+
LOGFILENAME = os.path.join(_LOGDIR, _LOGNAME)
58+
print(LOGFILENAME)
59+
if not os.path.exists(_LOGDIR):
60+
os.mkdir(_LOGDIR)
61+
return LOGFILENAME
62+
print(LOGFILENAME)
63+
64+
def createlogger(self,logfilename):
65+
logger= logging.getLogger()
66+
logger.setLevel(logging.INFO)
67+
handler = logging.FileHandler(logfilename)
68+
handler.setLevel(logging.INFO)
69+
formater = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
70+
handler.setFormatter(formater)
71+
logger.addHandler(handler)
72+
return logger
73+
74+
if __name__ == "__main__":
75+
glploger = Glp_Log('public-vpn.log')
76+
logfilename = glploger.createDir()
77+
logger = glploger.createlogger(logfilename)
78+
79+
app = ecsOper(logger)
80+
app.reboot_instance()
81+

0 commit comments

Comments
 (0)