Package cm_api :: Package endpoints :: Module parcels
[hide private]
[frames] | no frames]

Source Code for Module cm_api.endpoints.parcels

  1  # Licensed to Cloudera, Inc. under one 
  2  # or more contributor license agreements.  See the NOTICE file 
  3  # distributed with this work for additional information 
  4  # regarding copyright ownership.  Cloudera, Inc. licenses this file 
  5  # to you under the Apache License, Version 2.0 (the 
  6  # "License"); you may not use this file except in compliance 
  7  # with the License.  You may obtain a copy of the License at 
  8  # 
  9  #     http://www.apache.org/licenses/LICENSE-2.0 
 10  # 
 11  # Unless required by applicable law or agreed to in writing, software 
 12  # distributed under the License is distributed on an "AS IS" BASIS, 
 13  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 14  # See the License for the specific language governing permissions and 
 15  # limitations under the License. 
 16   
 17  from cm_api.endpoints.types import * 
 18   
 19  __docformat__ = "epytext" 
 20   
 21  PARCELS_PATH = "/clusters/%s/parcels" 
 22  PARCEL_PATH = "/clusters/%s/parcels/products/%s/versions/%s" 
 23   
24 -def get_parcel(resource_root, product, version, cluster_name="default"):
25 """ 26 Lookup a service by name 27 @param resource_root: The root Resource object. 28 @param name: Service name 29 @param cluster_name: Cluster name 30 @return: An ApiService object 31 """ 32 return _get_parcel(resource_root, PARCEL_PATH % (cluster_name, product, version))
33
34 -def _get_parcel(resource_root, path):
35 dic = resource_root.get(path) 36 return ApiParcel.from_json_dict(dic, resource_root)
37
38 -def get_all_parcels(resource_root, cluster_name="default", view=None):
39 """ 40 Get all parcels 41 @param resource_root: The root Resource object. 42 @param cluster_name: Cluster name 43 @return: A list of ApiParcel objects. 44 """ 45 dic = resource_root.get(PARCELS_PATH % (cluster_name,), 46 params=view and dict(view=view) or None) 47 return ApiList.from_json_dict(ApiParcel, dic, resource_root)
48
49 -class ApiParcelState(BaseApiObject):
50 """ 51 An object that represents the state of a parcel. 52 """ 53 _ATTRIBUTES = { 54 'progress' : ROAttr(), 55 'totalProgress' : ROAttr(), 56 'count' : ROAttr(), 57 'totalCount' : ROAttr(), 58 'warnings' : ROAttr(), 59 'errors' : ROAttr(), 60 } 61
62 - def __init__(self, resource_root):
63 BaseApiObject.init(self, resource_root)
64
65 - def __str__(self):
66 return "<ApiParcelState>: (progress: %s) (totalProgress: %s) (count: %s) (totalCount: %s)" % ( 67 self.progress, self.totalProgress, self.count, self.totalCount)
68
69 -class ApiParcel(BaseApiObject):
70 """ 71 An object that represents a parcel and allows administrative operations. 72 """ 73 _ATTRIBUTES = { 74 'product' : ROAttr(), 75 'version' : ROAttr(), 76 'stage' : ROAttr(), 77 'state' : ROAttr(ApiParcelState), 78 'clusterRef' : ROAttr(ApiClusterRef), 79 } 80
81 - def __init__(self, resource_root):
82 BaseApiObject.init(self, resource_root)
83
84 - def __str__(self):
85 return "<ApiParcel>: %s-%s (stage: %s) (state: %s) (cluster: %s)" % ( 86 self.product, self.version, self.stage, self.state, self._get_cluster_name())
87
88 - def _path(self):
89 """ 90 Return the API path for this service. 91 """ 92 return PARCEL_PATH % (self._get_cluster_name(), self.product, self.version)
93
94 - def _get_cluster_name(self):
95 if self.clusterRef: 96 return self.clusterRef.clusterName 97 return None
98
99 - def _cmd(self, cmd, data=None, params=None):
100 path = self._path() + '/commands/' + cmd 101 resp = self._get_resource_root().post(path, data=data, params=params) 102 cmd = ApiCommand.from_json_dict(resp, self._get_resource_root()) 103 if cmd.success: 104 self._update(_get_parcel(self._get_resource_root(), self._path())) 105 return cmd
106
107 - def start_download(self):
108 """ 109 Start the download of the parcel 110 111 @return: Reference to the completed command. 112 """ 113 return self._cmd('startDownload')
114
115 - def cancel_download(self):
116 """ 117 Cancels the parcel download. If the parcel is not 118 currently downloading an exception is raised. 119 120 @return: Reference to the completed command. 121 """ 122 return self._cmd('cancelDownload')
123
124 - def remove_download(self):
125 """ 126 Removes the downloaded parcel 127 128 @return: Reference to the completed command. 129 """ 130 return self._cmd('removeDownload')
131
132 - def start_distribution(self):
133 """ 134 Start the distribution of the parcel to all hosts 135 in the cluster. 136 137 @return: Reference to the completed command. 138 """ 139 return self._cmd('startDistribution')
140
141 - def cancel_distribution(self):
142 """ 143 Cancels the parcel distrubution. If the parcel is not 144 currently distributing an exception is raised. 145 146 @return: Reference to the completed command 147 """ 148 return self._cmd('cancelDistribution')
149
151 """ 152 Start the removal of the distribution of the parcel 153 from all the hosts in the cluster. 154 155 @return: Reference to the completed command. 156 """ 157 return self._cmd('startRemovalOfDistribution')
158
159 - def activate(self):
160 """ 161 Activate the parcel on all the hosts in the cluster. 162 163 @return: Reference to the completed command. 164 """ 165 return self._cmd('activate')
166
167 - def deactivate(self):
168 """ 169 Deactivates the parcel on all the hosts in the cluster. 170 171 @return: Reference to the completed command. 172 """ 173 return self._cmd('deactivate')
174