2525import json
2626import os
2727import logging
28+ import platform
29+ from shutil import copy
2830
2931import deployer_utils
3032from plugins .base_creator import Creator
@@ -35,12 +37,8 @@ class SparkStreamingCreator(Creator):
3537 def validate_component (self , component ):
3638 errors = []
3739 file_list = component ['component_detail' ]
38- if 'yarn-kill.py' not in file_list :
39- errors .append ('missing file yarn-kill.py' )
4040 if 'application.properties' not in file_list :
4141 errors .append ('missing file application.properties' )
42- if 'upstart.conf' not in file_list :
43- errors .append ('missing file upstart.conf' )
4442 if 'log4j.properties' not in file_list :
4543 errors .append ('missing file log4j.properties' )
4644 return errors
@@ -68,20 +66,55 @@ def _control_component(self, cmds):
6866
6967 def create_component (self , staged_component_path , application_name , component , properties ):
7068 logging .debug ("create_component: %s %s %s" , application_name , json .dumps (component ), properties )
71-
69+ distro = platform .dist ()
70+ redhat = distro [0 ] == 'redhat'
7271 remote_component_tmp_path = '%s/%s/%s' % (
7372 '/tmp/%s' % self ._namespace , application_name , component ['component_name' ])
7473 remote_component_install_path = '%s/%s/%s' % (
7574 '/opt/%s' % self ._namespace , application_name , component ['component_name' ])
75+ service_name = '%s-%s-%s' % (self ._namespace , application_name , component ['component_name' ])
7676
7777 key_file = self ._environment ['cluster_private_key' ]
7878 root_user = self ._environment ['cluster_root_user' ]
7979 target_host = 'localhost'
8080
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+ if 'component_py_files' not in properties :
84+ properties ['component_py_files' ] = ''
85+
86+ if 'upstart.conf' in component ['component_detail' ]:
87+ # old style applications for backward compatibility
88+ service_script = 'upstart.conf'
89+ service_script_install_path = '/etc/init/%s.conf' % service_name
90+ else :
91+ # new style applications that don't need to provide upstart.conf or yarn-kill.py
92+ if 'component_main_jar' in properties and 'component_main_class' not in properties :
93+ raise Exception ('properties.json must contain "main_class" for %s sparkStreaming %s' % (application_name , component ['component_name' ]))
94+
95+ java_app = None
96+ if 'component_main_jar' in properties :
97+ java_app = True
98+ elif 'component_main_py' in properties :
99+ java_app = False
100+ else :
101+ raise Exception ('properties.json must contain "main_jar or main_py" for %s sparkStreaming %s' % (application_name , component ['component_name' ]))
102+
103+ this_dir = os .path .dirname (os .path .realpath (__file__ ))
104+ copy (os .path .join (this_dir , 'yarn-kill.py' ), staged_component_path )
105+ distro = platform .dist ()
106+ if redhat :
107+ service_script = 'systemd.service.tpl' if java_app else 'systemd.service.py.tpl'
108+ service_script_install_path = '/usr/lib/systemd/system/%s.service' % service_name
109+ else :
110+ service_script = 'upstart.conf.tpl' if java_app else 'upstart.conf.py.tpl'
111+ service_script_install_path = '/etc/init/%s.conf' % service_name
112+ copy (os .path .join (this_dir , service_script ), staged_component_path )
113+
114+ self ._fill_properties (os .path .join (staged_component_path , service_script ), properties )
115+ self ._fill_properties (os .path .join (staged_component_path , 'log4j.properties' ), properties )
116+ self ._fill_properties (os .path .join (staged_component_path , 'application.properties' ), properties )
117+ self ._fill_properties (os .path .join (staged_component_path , 'yarn-kill.py' ), properties )
85118
86119 mkdircommands = []
87120 mkdircommands .append ('mkdir -p %s' % remote_component_tmp_path )
@@ -99,32 +132,36 @@ def create_component(self, staged_component_path, application_name, component, p
99132 ['sudo mkdir -p %s' % remote_component_install_path ,
100133 'sudo mv %s %s' % (remote_component_tmp_path + '/log4j.properties' , remote_component_install_path + '/log4j.properties' )])
101134
135+ if 'component_main_py' in properties :
136+ main_jar_name = None
137+ if 'component_main_jar' in properties :
138+ main_jar_name = properties ['component_main_jar' ]
139+ else :
140+ main_jar_name = '*.jar'
141+
102142 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 ))
143+ commands .append ('sudo cp %s/%s %s' % (remote_component_tmp_path , service_script , service_script_install_path ))
144+ commands .append ('sudo cp %s/* %s' % (remote_component_tmp_path , remote_component_install_path ))
145+ commands .append ('sudo chmod a+x %s/yarn-kill.py' % (remote_component_install_path ))
146+ if main_jar_name is not None :
147+ commands .append ('cd %s && sudo jar uf %s application.properties' % (remote_component_install_path , main_jar_name ))
113148 commands .append ('sudo rm -rf %s' % (remote_component_tmp_path ))
114149 deployer_utils .exec_ssh (target_host , root_user , key_file , commands )
115150
116151 undo_commands = []
117- undo_commands .append ('sudo initctl stop %s \n ' % service_name )
152+ undo_commands .append ('sudo service %s stop \n ' % service_name )
118153 undo_commands .append ('sudo rm -rf %s\n ' % remote_component_install_path )
119- undo_commands .append ('sudo rm %s\n ' % upstart_script )
154+ undo_commands .append ('sudo rm %s\n ' % service_script_install_path )
120155 logging .debug ("uninstall commands: %s" , undo_commands )
121156
122157 start_commands = []
123- start_commands .append ('sudo initctl start %s\n ' % service_name )
158+ if redhat :
159+ start_commands .append ('sudo systemctl daemon-reload\n ' )
160+ start_commands .append ('sudo service %s start\n ' % service_name )
124161 logging .debug ("start commands: %s" , start_commands )
125162
126163 stop_commands = []
127- stop_commands .append ('sudo initctl stop %s \n ' % service_name )
164+ stop_commands .append ('sudo service %s stop \n ' % service_name )
128165 logging .debug ("stop commands: %s" , stop_commands )
129166
130167 return {'ssh' : undo_commands , 'start_cmds' : start_commands , 'stop_cmds' : stop_commands }
0 commit comments