25
25
import json
26
26
import os
27
27
import logging
28
+ import platform
29
+ from shutil import copy
28
30
29
31
import deployer_utils
30
32
from plugins .base_creator import Creator
@@ -35,12 +37,8 @@ class SparkStreamingCreator(Creator):
35
37
def validate_component (self , component ):
36
38
errors = []
37
39
file_list = component ['component_detail' ]
38
- if 'yarn-kill.py' not in file_list :
39
- errors .append ('missing file yarn-kill.py' )
40
40
if 'application.properties' not in file_list :
41
41
errors .append ('missing file application.properties' )
42
- if 'upstart.conf' not in file_list :
43
- errors .append ('missing file upstart.conf' )
44
42
if 'log4j.properties' not in file_list :
45
43
errors .append ('missing file log4j.properties' )
46
44
return errors
@@ -68,20 +66,47 @@ def _control_component(self, cmds):
68
66
69
67
def create_component (self , staged_component_path , application_name , component , properties ):
70
68
logging .debug ("create_component: %s %s %s" , application_name , json .dumps (component ), properties )
71
-
69
+ distro = platform .dist ()
70
+ redhat = distro [0 ] == 'redhat'
72
71
remote_component_tmp_path = '%s/%s/%s' % (
73
72
'/tmp/%s' % self ._namespace , application_name , component ['component_name' ])
74
73
remote_component_install_path = '%s/%s/%s' % (
75
74
'/opt/%s' % self ._namespace , application_name , component ['component_name' ])
75
+ service_name = '%s-%s-%s' % (self ._namespace , application_name , component ['component_name' ])
76
76
77
77
key_file = self ._environment ['cluster_private_key' ]
78
78
root_user = self ._environment ['cluster_root_user' ]
79
79
target_host = 'localhost'
80
80
81
- self ._fill_properties ('%s/%s' % (staged_component_path , 'upstart.conf' ), properties )
82
- self ._fill_properties ('%s/%s' % (staged_component_path , 'log4j.properties' ), properties )
83
- self ._fill_properties ('%s/%s' % (staged_component_path , 'application.properties' ), properties )
84
- self ._fill_properties ('%s/%s' % (staged_component_path , 'yarn-kill.py' ), properties )
81
+ if 'component_spark_submit_args' not in properties :
82
+ properties ['component_spark_submit_args' ] = ''
83
+
84
+ if 'upstart.conf' in component ['component_detail' ]:
85
+ # old style applications for backward compatibility
86
+ service_script = 'upstart.conf'
87
+ service_script_install_path = '/etc/init/%s.conf' % service_name
88
+ else :
89
+ # new style applications that don't need to provide upstart.conf or yarn-kill.py
90
+ if 'component_main_class' not in properties :
91
+ raise Exception ('properties.json must contain "main_class" for %s sparkStreaming %s' % (application_name , component ['component_name' ]))
92
+ if 'component_main_jar' not in properties :
93
+ raise Exception ('properties.json must contain "main_jar" for %s sparkStreaming %s' % (application_name , component ['component_name' ]))
94
+
95
+ this_dir = os .path .dirname (os .path .realpath (__file__ ))
96
+ copy (os .path .join (this_dir , 'yarn-kill.py' ), staged_component_path )
97
+ distro = platform .dist ()
98
+ if redhat :
99
+ service_script = 'systemd.service.tpl'
100
+ service_script_install_path = '/usr/lib/systemd/system/%s.service' % service_name
101
+ else :
102
+ service_script = 'upstart.conf.tpl'
103
+ service_script_install_path = '/etc/init/%s.conf' % service_name
104
+ copy (os .path .join (this_dir , service_script ), staged_component_path )
105
+
106
+ self ._fill_properties (os .path .join (staged_component_path , service_script ), properties )
107
+ self ._fill_properties (os .path .join (staged_component_path , 'log4j.properties' ), properties )
108
+ self ._fill_properties (os .path .join (staged_component_path , 'application.properties' ), properties )
109
+ self ._fill_properties (os .path .join (staged_component_path , 'yarn-kill.py' ), properties )
85
110
86
111
mkdircommands = []
87
112
mkdircommands .append ('mkdir -p %s' % remote_component_tmp_path )
@@ -99,32 +124,33 @@ def create_component(self, staged_component_path, application_name, component, p
99
124
['sudo mkdir -p %s' % remote_component_install_path ,
100
125
'sudo mv %s %s' % (remote_component_tmp_path + '/log4j.properties' , remote_component_install_path + '/log4j.properties' )])
101
126
127
+ if 'component_main_jar' in properties :
128
+ main_jar_name = properties ['component_main_jar' ]
129
+ else :
130
+ main_jar_name = '*.jar'
131
+
102
132
commands = []
103
- service_name = '%s-%s-%s' % (self ._namespace , application_name , component ['component_name' ])
104
- upstart_script = '/etc/init/%s.conf' % service_name
105
- commands .append ('sudo cp %s/upstart.conf %s' %
106
- (remote_component_tmp_path , upstart_script ))
107
- commands .append ('sudo cp %s/* %s' %
108
- (remote_component_tmp_path , remote_component_install_path ))
109
- commands .append ('sudo chmod a+x %s/yarn-kill.py' %
110
- (remote_component_install_path ))
111
- commands .append ('cd %s && sudo jar uf *.jar application.properties' %
112
- (remote_component_install_path ))
133
+ commands .append ('sudo cp %s/%s %s' % (remote_component_tmp_path , service_script , service_script_install_path ))
134
+ commands .append ('sudo cp %s/* %s' % (remote_component_tmp_path , remote_component_install_path ))
135
+ commands .append ('sudo chmod a+x %s/yarn-kill.py' % (remote_component_install_path ))
136
+ commands .append ('cd %s && sudo jar uf %s application.properties' % (remote_component_install_path , main_jar_name ))
113
137
commands .append ('sudo rm -rf %s' % (remote_component_tmp_path ))
114
138
deployer_utils .exec_ssh (target_host , root_user , key_file , commands )
115
139
116
140
undo_commands = []
117
- undo_commands .append ('sudo initctl stop %s \n ' % service_name )
141
+ undo_commands .append ('sudo service %s stop \n ' % service_name )
118
142
undo_commands .append ('sudo rm -rf %s\n ' % remote_component_install_path )
119
- undo_commands .append ('sudo rm %s\n ' % upstart_script )
143
+ undo_commands .append ('sudo rm %s\n ' % service_script_install_path )
120
144
logging .debug ("uninstall commands: %s" , undo_commands )
121
145
122
146
start_commands = []
123
- start_commands .append ('sudo initctl start %s\n ' % service_name )
147
+ if redhat :
148
+ start_commands .append ('sudo systemctl daemon-reload\n ' )
149
+ start_commands .append ('sudo service %s start\n ' % service_name )
124
150
logging .debug ("start commands: %s" , start_commands )
125
151
126
152
stop_commands = []
127
- stop_commands .append ('sudo initctl stop %s \n ' % service_name )
153
+ stop_commands .append ('sudo service %s stop \n ' % service_name )
128
154
logging .debug ("stop commands: %s" , stop_commands )
129
155
130
156
return {'ssh' : undo_commands , 'start_cmds' : start_commands , 'stop_cmds' : stop_commands }
0 commit comments