/include/Image.h

https://github.com/swapniltamse/one · C Header · 395 lines · 168 code · 60 blank · 167 comment · 17 complexity · e35980904d44fa55f44b087eee61484f MD5 · raw file

  1. /* ------------------------------------------------------------------------ */
  2. /* Copyright 2002-2011, OpenNebula Project Leads (OpenNebula.org) */
  3. /* */
  4. /* Licensed under the Apache License, Version 2.0 (the "License"); you may */
  5. /* not use this file except in compliance with the License. You may obtain */
  6. /* a copy of the License at */
  7. /* */
  8. /* http://www.apache.org/licenses/LICENSE-2.0 */
  9. /* */
  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. /* -------------------------------------------------------------------------*/
  16. #ifndef IMAGE_H_
  17. #define IMAGE_H_
  18. #include "PoolSQL.h"
  19. #include "ImageTemplate.h"
  20. #include "NebulaLog.h"
  21. using namespace std;
  22. /**
  23. * The Image class.
  24. */
  25. class Image : public PoolObjectSQL
  26. {
  27. public:
  28. /**
  29. * Type of Images
  30. */
  31. enum ImageType
  32. {
  33. OS = 0, /** < Base OS image */
  34. CDROM = 1, /** < An ISO9660 image */
  35. DATABLOCK = 2 /** < User persistent data device */
  36. };
  37. /**
  38. * Image State
  39. */
  40. enum ImageState
  41. {
  42. INIT = 0, /** < Initialization state */
  43. READY = 1, /** < Image ready to use */
  44. USED = 2, /** < Image in use */
  45. DISABLED = 3, /** < Image can not be instantiated by a VM */
  46. LOCKED = 4, /** < FS operation for the Image in process */
  47. ERROR = 5 /** < Error state the operation FAILED*/
  48. };
  49. // *************************************************************************
  50. // Image Public Methods
  51. // *************************************************************************
  52. /**
  53. * Function to print the Image object into a string in XML format
  54. * @param xml the resulting XML string
  55. * @return a reference to the generated string
  56. */
  57. string& to_xml(string& xml) const;
  58. /**
  59. * Rebuilds the object from an xml formatted string
  60. * @param xml_str The xml-formatted string
  61. *
  62. * @return 0 on success, -1 otherwise
  63. */
  64. int from_xml(const string &xml_str);
  65. /**
  66. * Returns true if the image is persistent
  67. * @return true if the image is persistent
  68. */
  69. bool isPersistent()
  70. {
  71. return (persistent_img == 1);
  72. };
  73. /**
  74. * Returns the source path of the image
  75. * @return source of image
  76. */
  77. const string& get_source()
  78. {
  79. return source;
  80. }
  81. /**
  82. * Sets the source path of the image
  83. */
  84. void set_source(const string& _source)
  85. {
  86. source = _source;
  87. }
  88. /**
  89. * Sets the size for the image
  90. */
  91. void set_size(unsigned int _size_mb)
  92. {
  93. size_mb = _size_mb;
  94. }
  95. /**
  96. * Returns the type of the image
  97. * @return type
  98. */
  99. ImageType get_type()
  100. {
  101. return type;
  102. }
  103. /**
  104. * Returns the image state
  105. * @return state of image
  106. */
  107. ImageState get_state()
  108. {
  109. return state;
  110. }
  111. /**
  112. * Sets the image state
  113. * @param state of image
  114. */
  115. void set_state(ImageState _state)
  116. {
  117. state = _state;
  118. }
  119. /**
  120. *
  121. */
  122. int dec_running ()
  123. {
  124. return --running_vms;
  125. }
  126. /**
  127. *
  128. */
  129. int inc_running()
  130. {
  131. return ++running_vms;
  132. }
  133. /**
  134. *
  135. */
  136. int get_running()
  137. {
  138. return running_vms;
  139. }
  140. /**
  141. * Set enum type
  142. * @return 0 on success, -1 otherwise
  143. */
  144. int set_type(const string& _type)
  145. {
  146. int rc = 0;
  147. if ( _type == "OS" )
  148. {
  149. type = OS;
  150. }
  151. else if ( _type == "CDROM" )
  152. {
  153. type = CDROM;
  154. }
  155. else if ( _type == "DATABLOCK" )
  156. {
  157. type = DATABLOCK;
  158. }
  159. else
  160. {
  161. rc = -1;
  162. }
  163. return rc;
  164. }
  165. /**
  166. * Publish or unpublish an image
  167. * @param pub true to publish the image
  168. * @return 0 on success
  169. */
  170. int publish(bool pub)
  171. {
  172. int rc = -1;
  173. if (pub == true)
  174. {
  175. if (!isPersistent())
  176. {
  177. public_obj = 1;
  178. rc = 0;
  179. }
  180. }
  181. else
  182. {
  183. public_obj = 0;
  184. rc = 0;
  185. }
  186. return rc;
  187. }
  188. /**
  189. * Set/Unset an image as persistent
  190. * @param persistent true to make an image persistent
  191. * @param error_str Returns the error reason, if any
  192. *
  193. * @return 0 on success
  194. */
  195. int persistent(bool persis, string& error_str)
  196. {
  197. if ( running_vms != 0 )
  198. {
  199. goto error_vms;
  200. }
  201. if (persis == true)
  202. {
  203. if ( isPublic() )
  204. {
  205. goto error_public;
  206. }
  207. persistent_img = 1;
  208. }
  209. else
  210. {
  211. persistent_img = 0;
  212. }
  213. return 0;
  214. error_vms:
  215. error_str = "Image cannot be in 'used' state.";
  216. goto error_common;
  217. error_public:
  218. error_str = "Image cannot be public and persistent.";
  219. goto error_common;
  220. error_common:
  221. return -1;
  222. }
  223. /**
  224. * Modifies the given disk attribute adding the following attributes:
  225. * * SOURCE: the file-path.
  226. * * BUS: will only be set if the Image's definition includes it.
  227. * * TARGET: the value set depends on:
  228. * - OS images will be mounted at prefix + a: hda, sda.
  229. * - Prefix + b is reserved for the contex cdrom.
  230. * - CDROM images will be at prefix + c: hdc, sdc.
  231. * - Several DATABLOCK images can be mounted, they will be set to
  232. * prefix + (d + index) : hdd, hde, hdf...
  233. * @param disk attribute for the VM template
  234. * @param index number of datablock images used by the same VM. Will be
  235. * automatically increased.
  236. * @param img_type will be set to the used image's type
  237. */
  238. int disk_attribute(VectorAttribute * disk, int* index, ImageType* img_type);
  239. /**
  240. * Factory method for image templates
  241. */
  242. Template * get_new_template()
  243. {
  244. return new ImageTemplate;
  245. }
  246. private:
  247. // -------------------------------------------------------------------------
  248. // Friends
  249. // -------------------------------------------------------------------------
  250. friend class ImagePool;
  251. // -------------------------------------------------------------------------
  252. // Image Description
  253. // -------------------------------------------------------------------------
  254. /**
  255. * Type of the Image
  256. */
  257. ImageType type;
  258. /**
  259. * Persistency of the Image
  260. */
  261. int persistent_img;
  262. /**
  263. * Registration time
  264. */
  265. time_t regtime;
  266. /**
  267. * Path to the image
  268. */
  269. string source;
  270. /**
  271. * Size of the image in MB
  272. */
  273. unsigned int size_mb;
  274. /**
  275. * Image state
  276. */
  277. ImageState state;
  278. /**
  279. * Number of VMs using the image
  280. */
  281. int running_vms;
  282. // *************************************************************************
  283. // DataBase implementation (Private)
  284. // *************************************************************************
  285. /**
  286. * Execute an INSERT or REPLACE Sql query.
  287. * @param db The SQL DB
  288. * @param replace Execute an INSERT or a REPLACE
  289. * @return 0 on success
  290. */
  291. int insert_replace(SqlDB *db, bool replace);
  292. /**
  293. * Bootstraps the database table(s) associated to the Image
  294. */
  295. static void bootstrap(SqlDB * db)
  296. {
  297. ostringstream oss_image(Image::db_bootstrap);
  298. db->exec(oss_image);
  299. };
  300. /**
  301. * "Encrypts" the password with SHA1 digest
  302. * @param password
  303. * @return sha1 encrypted password
  304. */
  305. static string sha1_digest(const string& pass);
  306. protected:
  307. // *************************************************************************
  308. // Constructor
  309. // *************************************************************************
  310. Image(int uid,
  311. int gid,
  312. const string& uname,
  313. const string& gname,
  314. ImageTemplate* img_template);
  315. virtual ~Image();
  316. // *************************************************************************
  317. // DataBase implementation
  318. // *************************************************************************
  319. static const char * db_names;
  320. static const char * db_bootstrap;
  321. static const char * table;
  322. /**
  323. * Writes the Image in the database.
  324. * @param db pointer to the db
  325. * @return 0 on success
  326. */
  327. virtual int insert(SqlDB *db, string& error_str);
  328. /**
  329. * Writes/updates the Images data fields in the database.
  330. * @param db pointer to the db
  331. * @return 0 on success
  332. */
  333. virtual int update(SqlDB *db);
  334. };
  335. #endif /*IMAGE_H_*/