问题描述:
解决云主机临时关机,自动running 云主机,减少登机器操作,采用的模块是openstack 组件相关模块,需要在控制节点上开发
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
|
""
"
@Item : cheetah v1.0
@Author : william
@Group : System YunWei
@Date : 2017-03-15
@E-mail : swq.499809608@hotmail.com
@Funtion:
""
"
import
os,sys,
time
,socket,json,threading,traceback,shutil,requests
reload(sys)
sys.setdefaultencoding(
'utf8'
)
import
datetime
from keystoneclient.auth.identity
import
v2
from keystoneclient.v2_0
import
client as kclient
from novaclient
import
client as nclient
from cinderclient
import
client as cindercli
from keystoneclient
import
session
from oslo_log
import
log as logging
def LOG(level,info):
fp =
open
(
'/var/log/nova/shutdonw.log'
,
'a'
)
fp.write(
"%s %s %s \n"
%(
time
.ctime(),level,info))
fp.close()
class Openstack(object):
def __init__(self):
version =
"v1.0"
def get_node_instances(self):
ops_dict = dict()
auths = v2.Password(
auth_url =
'http://127.0.0.1:35357/v2.0/'
,
username =
'admin'
,
password =
'adminxxxx'
,
tenant_name =
'admin'
,
)
# session connect
try:
sess = session.Session(auth=auths)
nova_client = nclient.Client(
'2'
,session=sess)
except:
LOG(
"Error"
,
'OpenStack %s auth conn faild '
%(k))
# time sleep 3 ,get nova-manager service list result
instances = nova_client.servers.list(search_opts={
'all_tenants'
: 1})
for
ins
in
instances:
if
ins.status ==
'SHUTOFF'
:
time
.
sleep
(3)
ins.start()
LOG(
"INFO"
,
"%s,%s,%s"
%(ins.
id
,ins.name,ins.addresses))
if
__name__ ==
"__main__"
:
sc = Openstack()
sc.get_node_instances()
""
"
try:
pid = os.fork()
if
pid > 0 :
sys.
exit
(0)
os.setsid()
os.chdir(
'/'
)
sys.stdin =
open
(
"/dev/null"
,
"r+"
)
sys.stdout = os.dup(sys.stdin.fileno())
sys.stderr = os.dup(sys.stdin.fileno())
print
'yes'
while
True:
LOG(
"INFO"
,
"Shutting start"
)
time
.
sleep
(6)
sc = Openstack()
sc.get_node_instances()
except IOError,e:
print e
LOG(
"Error"
,traceback.format_exc())
""
"
|
本文转自 swq499809608 51CTO博客,原文链接:http://blog.51cto.com/swq499809608/1962082