1 ##############################################################################
2 # Copyright 2017 Parker Berberian and Others #
4 # Licensed under the Apache License, Version 2.0 (the "License"); #
5 # you may not use this file except in compliance with the License. #
6 # You may obtain a copy of the License at #
8 # http://www.apache.org/licenses/LICENSE-2.0 #
10 # Unless required by applicable law or agreed to in writing, software #
11 # distributed under the License is distributed on an "AS IS" BASIS, #
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
13 # See the License for the specific language governing permissions and #
14 # limitations under the License. #
15 ##############################################################################
21 from st2actions.runners.pythonrunner import Action
24 class FogAction(Action):
26 This class talks with the REST web api for the FOG server.
28 def __init__(self, config=None):
29 self.baseURL = config['fog']['address']
30 self.fogKey = config['fog']['api_key']
31 self.userKey = config['fog']['user_key']
34 def updateHeader(self):
36 recreates the http header used to talk to the fog api
39 self.header['fog-api-token'] = self.fogKey
40 self.header['fog-user-token'] = self.userKey
42 def getImageID(self, img=None, os=None, host=None):
44 returns the numerical id associated with the given img name or
45 operating system. If both are given, the img gets priority.
46 if img is a number, it is assumed to be a valid id
48 # st2 will promote an empty arg to the str "None" :(
49 if not img or img == "None":
50 return self.getImageIDFromOS(os, host)
54 url = self.baseURL+"image"
55 images = requests.get(url=url, headers=self.header)
56 images = images.json()['images']
58 if img == image['name']:
62 def getImageIDFromOS(self, os, host):
63 enum = {"ubuntu": "ubuntu_image",
64 "centos": "centos_image",
68 if os not in enum.keys():
70 host_dict = json.loads(
71 self.action_service.get_value(name=host, local=False)
73 return int(host_dict[enum[os]])
75 def delTask(self, hostNum):
77 Tries to delete an existing task for the host
78 with hostNum as a host number
81 url = self.baseURL+'fog/host/'+str(hostNum)+'/cancel'
82 req = requests.delete(url, headers=self.header)
83 if req.status_code == 200:
84 self.logger.info("%s", "successfully deleted image task")
86 self.logger.exception("Failed to delete the imaging task!")
88 def getHostNumber(self, hostname):
90 returns the host number of given host
93 req = requests.get(self.baseURL+"host", headers=self.header)
95 if hostData is not None:
96 for hostDict in hostData['hosts']:
97 if hostname == hostDict['name']:
101 self.logger.exception('%s', "Failed to connect to the FOG server")
103 def request(self, url, data=None, method="get"):
105 return self.dataRequest(url, data, method=method)
107 response = requests.get(url, headers=self.header)
108 return response.json()
110 self.logger.exception("Failed to reach FOG at %s", url)
113 def dataRequest(self, url, data, method="post"):
115 "post": requests.post,
119 return methods[method](url, json=data, headers=self.header)
121 self.logger.exception("Failed to reach FOG at %s", url)
124 def getFogHost(self, host):
125 hostData = self.action_service.get_value(host, local=False)
126 return json.loads(hostData)['fog_name']
128 def waitForTask(self, taskID):
130 Watches a task and waits for it to finish (disapear).
131 There may be a smarter way to do this and track errors,
132 but st2 will timeout for me if something goes wrong
134 task = self.getTask(taskID)
137 task = self.getTask(taskID)
139 def getAllTasks(self):
141 tasks = requests.get(
142 self.baseURL+'task/current',
149 def getTask(self, taskID):
150 for task in self.getAllTasks():
151 if task['id'] == taskID: