nuri 0.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (169) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +6 -0
  3. data/.travis.yml +12 -0
  4. data/CHANGELOG +146 -0
  5. data/Gemfile +3 -0
  6. data/LICENSE +28 -0
  7. data/README.md +64 -0
  8. data/Rakefile +15 -0
  9. data/VERSION +1 -0
  10. data/bin/delete_modules +11 -0
  11. data/bin/install_agent +18 -0
  12. data/bin/install_module +65 -0
  13. data/bin/nuri +519 -0
  14. data/bin/nuri.old +183 -0
  15. data/bin/push_model +16 -0
  16. data/examples/.gitignore +3 -0
  17. data/examples/bonfire.sfp +95 -0
  18. data/examples/bonfire/epcc.sfp +43 -0
  19. data/examples/bonfire/epcc0.sfp +49 -0
  20. data/examples/bonfire/epcc2.sfp +52 -0
  21. data/examples/bonfire/epcc2a.sfp +25 -0
  22. data/examples/bonfire/inria.sfp +72 -0
  23. data/examples/bonfire/inria0.sfp +49 -0
  24. data/examples/bonfire/inria2.sfp +71 -0
  25. data/examples/bonfire/inria2a.sfp +44 -0
  26. data/examples/bonfire/inria2b.sfp +54 -0
  27. data/examples/bonfire/inria2c.sfp +62 -0
  28. data/examples/bonfire/inria2d.sfp +71 -0
  29. data/examples/bonfire/inria2e.sfp +80 -0
  30. data/examples/bonfire/main.sfp +33 -0
  31. data/examples/bonfire/old/bonfire-1-1-1.sfp +76 -0
  32. data/examples/bonfire/old/bonfire-1-10-1.sfp +77 -0
  33. data/examples/bonfire/old/bonfire-1-2-1.sfp +58 -0
  34. data/examples/bonfire/old/bonfire-1-3-1.sfp +61 -0
  35. data/examples/bonfire/old/bonfire-1-4-1.sfp +64 -0
  36. data/examples/bonfire/old/bonfire-1-5-1.sfp +67 -0
  37. data/examples/bonfire/old/bonfire-1-6-1.sfp +82 -0
  38. data/examples/bonfire/old/bonfire-1-7-1.sfp +82 -0
  39. data/examples/bonfire/old/bonfire-1-8-1.sfp +79 -0
  40. data/examples/bonfire/old/bonfire-1-9-1.sfp +83 -0
  41. data/examples/bonfire/old/wp-test1a.sfp +38 -0
  42. data/examples/bonfire/old/wp-test1b.sfp +18 -0
  43. data/examples/bonfire/old/wp-test1c.sfp +7 -0
  44. data/examples/bonfire/old/wp-test2.sfp +47 -0
  45. data/examples/bonfire/old3/bonfire-epcc.sfp +57 -0
  46. data/examples/bonfire/old3/bonfire-inria.sfp +72 -0
  47. data/examples/bonfire/old3/bonfire-master.sfp +18 -0
  48. data/examples/bonfire/old3/bonfire.sfp +23 -0
  49. data/examples/bonfire/old3/bonfire2.sfp +49 -0
  50. data/examples/bonfire/old3/bonfire3.sfp +76 -0
  51. data/examples/bonfire/old3/bonfire4.sfp +78 -0
  52. data/examples/bonfire/old3/bonfire5.sfp +34 -0
  53. data/examples/bonfire/old3/bonfire5b.sfp +84 -0
  54. data/examples/bonfire/old3/hpvm6.sfp +22 -0
  55. data/examples/bonfire/old3/model.json +1 -0
  56. data/examples/bonfire/old3/test0.sfp +16 -0
  57. data/examples/bonfire/old3/test1.sfp +5 -0
  58. data/examples/bonfire/old3/test10.sfp +5 -0
  59. data/examples/bonfire/old3/test2.sfp +18 -0
  60. data/examples/bonfire/old3/test3.sfp +10 -0
  61. data/examples/bonfire/old3/test4.sfp +11 -0
  62. data/examples/bonfire/old3/test5.sfp +18 -0
  63. data/examples/bonfire/old3/test6.sfp +19 -0
  64. data/examples/bonfire/old3/test7.sfp +34 -0
  65. data/examples/bonfire/old3/test8.sfp +5 -0
  66. data/examples/bonfire/old3/test9.sfp +16 -0
  67. data/examples/bonfire/old3/wordpress-test-cluster.sfp +38 -0
  68. data/examples/bonfire/old3/wordpress-test.sfp +22 -0
  69. data/examples/bonfire/old3/wp-test-2.sfp +49 -0
  70. data/examples/bonfire/test.sfp +13 -0
  71. data/examples/generator.rb +66 -0
  72. data/examples/hadoop2.sfp +20 -0
  73. data/examples/hpcloud.sfp +18 -0
  74. data/examples/run.rb +17 -0
  75. data/examples/test.inc +0 -0
  76. data/examples/test.sfp +11 -0
  77. data/lib/naas/d3.js +5 -0
  78. data/lib/naas/d3.v3.min.js +5 -0
  79. data/lib/naas/index.css +0 -0
  80. data/lib/naas/index.html +18 -0
  81. data/lib/naas/index.js +18 -0
  82. data/lib/naas/jquery-1.10.2.min.js +6 -0
  83. data/lib/naas/jquery.js +6 -0
  84. data/lib/naas/naas.rb +160 -0
  85. data/lib/nuri.rb +62 -0
  86. data/lib/nuri/choreographer.rb +151 -0
  87. data/lib/nuri/constraint_helper.rb +9 -0
  88. data/lib/nuri/directory.rb +40 -0
  89. data/lib/nuri/master.rb +725 -0
  90. data/lib/nuri/net_helper.rb +65 -0
  91. data/lib/nuri/orchestrator.rb +224 -0
  92. data/lib/nuri/server.rb +212 -0
  93. data/modules/.gitignore +4 -0
  94. data/modules/apache/apache.rb +255 -0
  95. data/modules/apache/apache.rb.old +167 -0
  96. data/modules/apache/apache.sfp +146 -0
  97. data/modules/apache/apache.sfp.future +100 -0
  98. data/modules/apache/load_balancer +20 -0
  99. data/modules/apache/model.json +1 -0
  100. data/modules/apache/test.sfp +8 -0
  101. data/modules/aptpackage/aptpackage.rb +82 -0
  102. data/modules/aptpackage/aptpackage.sfp +5 -0
  103. data/modules/bonfire/.gitignore +2 -0
  104. data/modules/bonfire/README.md +12 -0
  105. data/modules/bonfire/bonfire.rb +60 -0
  106. data/modules/bonfire/bonfire.sfp +9 -0
  107. data/modules/bonfire/config.yml +4 -0
  108. data/modules/bonfire/helper.rb +149 -0
  109. data/modules/bonfire/stresstest.rb +144 -0
  110. data/modules/bonfire/test.sfp +8 -0
  111. data/modules/client/client.rb +22 -0
  112. data/modules/client/client.sfp +14 -0
  113. data/modules/cloud/cloud.rb +11 -0
  114. data/modules/cloud/cloud.sfp +26 -0
  115. data/modules/file/file.rb +91 -0
  116. data/modules/file/file.sfp +9 -0
  117. data/modules/hadoop1/core-site.xml +17 -0
  118. data/modules/hadoop1/hadoop-env.sh +55 -0
  119. data/modules/hadoop1/hadoop1.rb +384 -0
  120. data/modules/hadoop1/hadoop1.sfp +93 -0
  121. data/modules/hadoop1/hdfs-site.xml +16 -0
  122. data/modules/hadoop1/mapred-site.xml +17 -0
  123. data/modules/hadoop2/core-site.xml +31 -0
  124. data/modules/hadoop2/hadoop-env.sh +77 -0
  125. data/modules/hadoop2/hadoop2.rb +401 -0
  126. data/modules/hadoop2/hadoop2.sfp +114 -0
  127. data/modules/hadoop2/hdfs-site.xml +47 -0
  128. data/modules/hadoop2/mapred-site.xml +71 -0
  129. data/modules/hadoop2/ports +14 -0
  130. data/modules/hadoop2/yarn-env.sh +112 -0
  131. data/modules/hadoop2/yarn-site.xml +107 -0
  132. data/modules/hpcloud/.gitignore +2 -0
  133. data/modules/hpcloud/README.md +16 -0
  134. data/modules/hpcloud/config.yml +3 -0
  135. data/modules/hpcloud/example.sfp +18 -0
  136. data/modules/hpcloud/hpcloud.rb +241 -0
  137. data/modules/hpcloud/hpcloud.sfp +22 -0
  138. data/modules/hpcloud/test.sfp +5 -0
  139. data/modules/install_module +65 -0
  140. data/modules/machine/machine.rb +95 -0
  141. data/modules/machine/machine.sfp +9 -0
  142. data/modules/mockcloud/mockcloud.rb +20 -0
  143. data/modules/mockcloud/mockcloud.sfp +6 -0
  144. data/modules/mysql/mysql.rb +118 -0
  145. data/modules/mysql/mysql.sfp +38 -0
  146. data/modules/mysql/test.sfp +3 -0
  147. data/modules/node/node.rb +8 -0
  148. data/modules/node/node.sfp +7 -0
  149. data/modules/object/object.rb +7 -0
  150. data/modules/object/object.sfp +1 -0
  151. data/modules/os/os.rb +38 -0
  152. data/modules/os/os.sfp +11 -0
  153. data/modules/package/package.rb +26 -0
  154. data/modules/package/package.sfp +22 -0
  155. data/modules/package/test.sfp +6 -0
  156. data/modules/service/model.json +1 -0
  157. data/modules/service/service.rb +50 -0
  158. data/modules/service/service.sfp +46 -0
  159. data/modules/service/test.sfp +6 -0
  160. data/modules/tarpackage/tarpackage.rb +93 -0
  161. data/modules/tarpackage/tarpackage.sfp +5 -0
  162. data/modules/vm/vm.rb +8 -0
  163. data/modules/vm/vm.sfp +18 -0
  164. data/modules/wordpress/wordpress.rb +98 -0
  165. data/modules/wordpress/wordpress.sfp +34 -0
  166. data/modules/wordpresscluster/wordpresscluster.rb +150 -0
  167. data/modules/wordpresscluster/wordpresscluster.sfp +74 -0
  168. data/nuri.gemspec +26 -0
  169. metadata +281 -0
@@ -0,0 +1,9 @@
1
+ include "../node/node.sfp"
2
+ include "../os/os.sfp"
3
+
4
+ schema Machine extends Node {
5
+ created is true
6
+ hypervisor : String
7
+
8
+ os isa OS
9
+ }
@@ -0,0 +1,20 @@
1
+ class Sfp::Module::MockCloud
2
+ include Sfp::Resource
3
+
4
+ def update_state
5
+ @state['running'] = @model['running']
6
+ @state['description'] = @model['description']
7
+ @state['vms'] = {}
8
+ end
9
+
10
+ def create_vm(p={})
11
+ Sfp::Agent.logger.info p.inspect
12
+ model = resolve_model(p['vm'])
13
+ Sfp::Agent.logger.info "cpu=#{model['cpu']} memory=#{model['memory']}"
14
+ false
15
+ end
16
+
17
+ def delete_vm(p={})
18
+ false
19
+ end
20
+ end
@@ -0,0 +1,6 @@
1
+ include "../cloud/cloud.sfp"
2
+
3
+ schema MockCloud extends Cloud {
4
+ running is true
5
+ final description is "Mock cloud for testing"
6
+ }
@@ -0,0 +1,118 @@
1
+ require 'augeas'
2
+
3
+ require File.expand_path(File.dirname(__FILE__)) + '/../service/service.rb'
4
+
5
+ class Sfp::Module::Mysql < Sfp::Module::Service
6
+ include Sfp::Resource
7
+
8
+ def update_state
9
+ # call superclass update_state method
10
+ self.class.superclass.instance_method(:update_state).bind(self).call
11
+
12
+ # port
13
+ data = (File.file?("/etc/mysql/my.cnf") ? `/bin/grep -e "^port" /etc/mysql/my.cnf` : "")
14
+ @state['port'] = (data.length > 0 ? data.split('=')[1].strip.to_i : 3306)
15
+
16
+ # root password
17
+ if File.file?('/etc/mysql/nuri.cnf')
18
+ @state["root_password"] = `cat /etc/mysql/nuri.cnf 2>/dev/null`.to_s.sub(/\n$/,'')
19
+ else
20
+ @state['root_password'] = 'mysql'
21
+ end
22
+
23
+ # can be accessed from outside?
24
+ if @state['installed']
25
+ data = `grep '^bind-address' /etc/mysql/my.cnf 2>/dev/null`
26
+ @state['public'] = (data.length <= 0)
27
+ else
28
+ @state['public'] = false
29
+ end
30
+ end
31
+
32
+ def exec_seq(*commands)
33
+ commands.each { |c| raise Exception, "Cannot execute: #{c}" if !system(c) }
34
+ end
35
+
36
+ def install(params={})
37
+ begin
38
+ system('timeout 20s dpkg --configure -a')
39
+ system('apt-get -y --purge autoremove')
40
+ system('apt-get -y update')
41
+ exec_seq 'echo mysql-server mysql-server/root_password select mysql | debconf-set-selections',
42
+ 'echo mysql-server mysql-server/root_password_again select mysql | debconf-set-selections',
43
+ "apt-get -y install #{@model['package_name']}",
44
+ 'echo "\n[mysqld]\nmax_connect_errors = 10000" >> /etc/mysql/my.cnf',
45
+ 'service mysql stop',
46
+ '/bin/echo mysql > /etc/mysql/nuri.cnf',
47
+ '/bin/chmod 0400 /etc/mysql/nuri.cnf'
48
+ return true
49
+ rescue Exception => e
50
+ Sfp::Agent.logger.error "#{e}\n#{e.backtrace.join("\n")}"
51
+ end
52
+ false
53
+ end
54
+
55
+ def uninstall(params={})
56
+ system('/bin/rm -f /etc/mysql/nuri.cnf') if File.exist?('/etc/mysql/nuri.cnf')
57
+ system('timeout 20s dpkg --configure -a')
58
+ exec_seq 'apt-get remove --purge -y mysql-server*',
59
+ 'apt-get autoremove --purge -y',
60
+ 'apt-get autoremove --purge -y'
61
+
62
+ #if Sfp::Module::Package.uninstall('mysql-server*')
63
+ #result = self.class.superclass.instance_method(:uninstall).bind(self).call
64
+ #if result == false
65
+ # system('apt-get remove -y mysql*; apt-get autoremove -y; apt-get autoremove -y')
66
+ #end
67
+ system('/bin/rm -rf /etc/mysql') if File.exist?('/etc/mysql')
68
+ true
69
+ #else
70
+ # false
71
+ #end
72
+ #return result
73
+ #false
74
+ end
75
+
76
+ def set_port(params={})
77
+ p = params['target']
78
+ Augeas::open do |aug|
79
+ paths = aug.match("/files/etc/mysql/my.cnf/*/port")
80
+ paths.each { |path|
81
+ aug.set(path, p.to_s)
82
+ }
83
+ return aug.save
84
+ end
85
+ false
86
+ end
87
+
88
+ def set_public(params={})
89
+ if params['pub']
90
+ cmd = '/bin/sed -i "s/^bind\-address/#bind\-address/g" /etc/mysql/my.cnf'
91
+ else
92
+ cmd = '/bin/sed -i "s/^#bind\-address/bind\-address/g" /etc/mysql/my.cnf'
93
+ end
94
+ return false if not system(cmd)
95
+ if Sfp::Module::Service.running?(@model['service_name'])
96
+ return (self.stop and self.start)
97
+ end
98
+ true
99
+ end
100
+
101
+ def set_root_password(params={})
102
+ passwd = params['passwd'].to_s
103
+ system('/bin/chmod 0600 /etc/mysql/nuri.cnf')
104
+ oldpass = `cat /etc/mysql/nuri.cnf`.to_s.sub(/\n$/,'').sub(/"/,'\"')
105
+ passwd.sub!(/"/,'\"')
106
+ return (system("mysqladmin -u root -p\"#{oldpass}\" password \"#{passwd}\"") and
107
+ system("/bin/echo \"#{passwd}\" > /etc/mysql/nuri.cnf") and
108
+ system('/bin/chmod 0400 /etc/mysql/nuri.cnf'))
109
+ end
110
+
111
+ def set_service_name(p={})
112
+ true
113
+ end
114
+
115
+ def set_package_name(p={})
116
+ true
117
+ end
118
+ end
@@ -0,0 +1,38 @@
1
+ include "../service/service.sfp"
2
+
3
+ schema Mysql extends Service {
4
+ package_name = "mysql-server"
5
+ service_name = "mysql"
6
+ port : Int
7
+ root_password : String
8
+ public : Bool
9
+
10
+ sub set_root_password (passwd isref String) {
11
+ condition {
12
+ this.running = true
13
+ }
14
+ effect {
15
+ this.root_password = passwd
16
+ }
17
+ }
18
+
19
+ sub set_public (pub isref Boolean) {
20
+ condition {
21
+ this.installed = true
22
+ this.running = false
23
+ }
24
+ effect {
25
+ this.public = pub
26
+ }
27
+ }
28
+
29
+ sub set_port (target isref Integer) {
30
+ condition {
31
+ this.installed = true
32
+ this.running = false
33
+ }
34
+ effect {
35
+ this.port = target
36
+ }
37
+ }
38
+ }
@@ -0,0 +1,3 @@
1
+ include "mysql.sfp"
2
+
3
+ mysql isa Mysql
@@ -0,0 +1,8 @@
1
+ class Sfp::Module::Node
2
+ include Sfp::Resource
3
+
4
+ def update_state
5
+ @state['sfpAddress'] = @model['sfpAddress']
6
+ @state['sfpPort'] = @model['sfpPort']
7
+ end
8
+ end
@@ -0,0 +1,7 @@
1
+ include "../os/os.sfp"
2
+ include "../cloud/cloud.sfp"
3
+
4
+ schema Node {
5
+ final sfpAddress is ""
6
+ final sfpPort is 1314
7
+ }
@@ -0,0 +1,7 @@
1
+ class Sfp::Module::Object
2
+ include Sfp::Resource
3
+
4
+ def update_state
5
+ to_model
6
+ end
7
+ end
@@ -0,0 +1 @@
1
+ schema Object { }
data/modules/os/os.rb ADDED
@@ -0,0 +1,38 @@
1
+ require 'rubygems'
2
+ require 'json'
3
+
4
+ class Sfp::Module::OS
5
+ include Sfp::Resource
6
+
7
+ def update_state
8
+ @state['running'] = true
9
+ #@state['auto_upgrade'] = @model['auto_upgrade']
10
+
11
+ # get memory info
12
+ if `which free`.strip != ''
13
+ mem = `free`.split("\n")[1].split(" ")
14
+ @state["memory_total"] = mem[1].to_i
15
+ @state["memory_free"] = mem[3].to_i
16
+ else
17
+ @state["memory_total"] = @state["memory_free"] = -1
18
+ end
19
+
20
+ # get platform, architecture, kernel version
21
+ @state["type"] = `uname -s`.strip
22
+ @state["version"] = `uname -r`.strip
23
+ @state["arch"] = `uname -p`.strip
24
+ @state["platform"] = (File.exist?('/etc/issue') ? `cat /etc/issue`.strip : '')
25
+ end
26
+
27
+ def apply(p={})
28
+ self.upgrade if @model['auto_upgrade']
29
+ end
30
+
31
+ def upgrade(p={})
32
+ return !!system('apt-get update; apt-get upgrade -y')
33
+ end
34
+
35
+ def stop(p={})
36
+ return !!system('/sbin/shutdown -h now')
37
+ end
38
+ end
data/modules/os/os.sfp ADDED
@@ -0,0 +1,11 @@
1
+ schema OS {
2
+ running : Bool
3
+ //auto_upgrade : Bool
4
+
5
+ sub shutdown {
6
+ condition { }
7
+ effect {
8
+ this.running = false
9
+ }
10
+ }
11
+ }
@@ -0,0 +1,26 @@
1
+ class Sfp::Module::Package
2
+ include Sfp::Resource
3
+
4
+ def update_state
5
+ to_model
6
+
7
+ @state['installed'] = installed?
8
+ @state['version'] = version?
9
+ end
10
+
11
+ def installed?
12
+ false
13
+ end
14
+
15
+ def version?
16
+ ""
17
+ end
18
+
19
+ def install(p={})
20
+ false
21
+ end
22
+
23
+ def uninstall(p={})
24
+ false
25
+ end
26
+ end
@@ -0,0 +1,22 @@
1
+ schema Package {
2
+ installed : Bool
3
+
4
+ final package_name = ""
5
+ final version : String
6
+ final source = ""
7
+
8
+ synchronized sub install {
9
+ effect {
10
+ this.installed = true
11
+ }
12
+ }
13
+
14
+ synchronized sub uninstall {
15
+ condition {
16
+ this.installed = true
17
+ }
18
+ effect {
19
+ this.installed = false
20
+ }
21
+ }
22
+ }
@@ -0,0 +1,6 @@
1
+ include "package.sfp"
2
+
3
+ apache isa Package {
4
+ package_name = "apache2"
5
+ installed = true
6
+ }
@@ -0,0 +1 @@
1
+ {"Object":{"_self":"Object","_context":"class"},"Package":{"_self":"Package","_context":"class","package_name":"","installed":false,"version":"","install":{"_self":"install","_context":"procedure","_cost":1,"_condition":{"_context":"constraint","_type":"and","$.installed":{"_context":"constraint","_type":"equals","_value":false}},"_effect":{"_context":"effect","_type":"and","$.installed":{"_context":"mutation","_type":"equals","_value":true}}},"uninstall":{"_self":"uninstall","_context":"procedure","_cost":1,"_condition":{"_context":"constraint","_type":"and","$.installed":{"_context":"constraint","_type":"equals","_value":true}},"_effect":{"_context":"effect","_type":"and","$.installed":{"_context":"mutation","_type":"equals","_value":false}}},"_extends":"$.Object","_super":["$.Object"]},"Service":{"_self":"Service","_context":"class","_extends":"$.Package","service_name":"","running":false,"uninstall":{"_self":"uninstall","_context":"procedure","_cost":1,"_condition":{"_context":"constraint","_type":"and","$.this.running":{"_context":"constraint","_type":"equals","_value":false},"$.this.installed":{"_context":"constraint","_type":"equals","_value":true}},"_effect":{"_context":"effect","_type":"and","$.this.installed":{"_context":"mutation","_type":"equals","_value":false}}},"start":{"_self":"start","_context":"procedure","_cost":1,"_condition":{"_context":"constraint","_type":"and","$.this.installed":{"_context":"constraint","_type":"equals","_value":true},"$.this.running":{"_context":"constraint","_type":"equals","_value":false}},"_effect":{"_context":"effect","_type":"and","$.this.running":{"_context":"mutation","_type":"equals","_value":true}}},"stop":{"_self":"stop","_context":"procedure","_cost":1,"_condition":{"_context":"constraint","_type":"and","$.this.running":{"_context":"constraint","_type":"equals","_value":true}},"_effect":{"_context":"effect","_type":"and","$.this.running":{"_context":"mutation","_type":"equals","_value":false}}},"package_name":"","installed":false,"version":"","install":{"_self":"install","_context":"procedure","_cost":1,"_condition":{"_context":"constraint","_type":"and","$.installed":{"_context":"constraint","_type":"equals","_value":false}},"_effect":{"_context":"effect","_type":"and","$.installed":{"_context":"mutation","_type":"equals","_value":true}}},"_super":["$.Object","$.Package"]},"apache":{"_self":"apache","_context":"object","_isa":"$.Service","_is_array":false,"service_name":"apache2","running":false,"uninstall":{"_self":"uninstall","_context":"procedure","_cost":1,"_condition":{"_context":"constraint","_type":"and","$.this.running":{"_context":"constraint","_type":"equals","_value":false},"$.this.installed":{"_context":"constraint","_type":"equals","_value":true}},"_effect":{"_context":"effect","_type":"and","$.this.installed":{"_context":"mutation","_type":"equals","_value":false}}},"start":{"_self":"start","_context":"procedure","_cost":1,"_condition":{"_context":"constraint","_type":"and","$.this.installed":{"_context":"constraint","_type":"equals","_value":true},"$.this.running":{"_context":"constraint","_type":"equals","_value":false}},"_effect":{"_context":"effect","_type":"and","$.this.running":{"_context":"mutation","_type":"equals","_value":true}}},"stop":{"_self":"stop","_context":"procedure","_cost":1,"_condition":{"_context":"constraint","_type":"and","$.this.running":{"_context":"constraint","_type":"equals","_value":true}},"_effect":{"_context":"effect","_type":"and","$.this.running":{"_context":"mutation","_type":"equals","_value":false}}},"package_name":"apache2","installed":false,"version":"","install":{"_self":"install","_context":"procedure","_cost":1,"_condition":{"_context":"constraint","_type":"and","$.installed":{"_context":"constraint","_type":"equals","_value":false}},"_effect":{"_context":"effect","_type":"and","$.installed":{"_context":"mutation","_type":"equals","_value":true}}},"_classes":["$.Object","$.Package","$.Service"]}}
@@ -0,0 +1,50 @@
1
+ require File.expand_path(File.dirname(__FILE__)) + "/../aptpackage/aptpackage.rb"
2
+
3
+ class Sfp::Module::Service < Sfp::Module::AptPackage
4
+ def update_state
5
+ to_model
6
+ if @model.is_a?(Hash)
7
+ @state['installed'] = installed?
8
+ @state['version'] = version?
9
+ @state['running'] = Sfp::Module::Service.running?(@model['service_name'])
10
+ end
11
+ end
12
+
13
+ ##############################
14
+ #
15
+ # Action methods (see TarPackage.sfp)
16
+ #
17
+ ##############################
18
+
19
+ def start(p={})
20
+ service = @model['service_name'].to_s.strip
21
+ return false if service.length <= 0
22
+ return true if Sfp::Module::Service.running?(service)
23
+ system("sudo service #{service} start")
24
+ sleep 1
25
+ return Sfp::Module::Service.running?(service)
26
+ end
27
+
28
+ def stop(p={})
29
+ service = @model['service_name'].to_s.strip
30
+ return false if service.length <= 0
31
+ return true if not Sfp::Module::Service.running?(service)
32
+ system("sudo service #{service} stop")
33
+ sleep 1
34
+ return !Sfp::Module::Service.running?(service)
35
+ end
36
+
37
+
38
+ ##############################
39
+ #
40
+ # Helper methods
41
+ #
42
+ ##############################
43
+
44
+ def self.running?(service)
45
+ service = service.to_s
46
+ return false if service.length <= 0
47
+ data = `service #{service} status 2>/dev/null`.to_s.downcase
48
+ return !!(data =~ /is running/ or data =~ /start\/running/ or data =~ /uptime/)
49
+ end
50
+ end
@@ -0,0 +1,46 @@
1
+ include "../aptpackage/aptpackage.sfp"
2
+
3
+ schema Service extends AptPackage {
4
+ running : Bool
5
+
6
+ final service_name = ""
7
+
8
+ synchronized sub install {
9
+ condition {
10
+ this.installed != true
11
+ }
12
+ effect {
13
+ this.installed = true
14
+ this.running = false
15
+ }
16
+ }
17
+
18
+ synchronized sub uninstall {
19
+ condition {
20
+ this.running = false
21
+ this.installed = true
22
+ }
23
+ effect {
24
+ this.installed = false
25
+ }
26
+ }
27
+
28
+ sub start {
29
+ condition {
30
+ this.running != true
31
+ this.installed = true
32
+ }
33
+ effect {
34
+ this.running = true
35
+ }
36
+ }
37
+
38
+ sub stop {
39
+ condition {
40
+ this.running = true
41
+ }
42
+ effect {
43
+ this.running = false
44
+ }
45
+ }
46
+ }