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,55 @@ 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
+ 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 )
85
118
86
119
mkdircommands = []
87
120
mkdircommands .append ('mkdir -p %s' % remote_component_tmp_path )
@@ -99,32 +132,36 @@ def create_component(self, staged_component_path, application_name, component, p
99
132
['sudo mkdir -p %s' % remote_component_install_path ,
100
133
'sudo mv %s %s' % (remote_component_tmp_path + '/log4j.properties' , remote_component_install_path + '/log4j.properties' )])
101
134
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
+
102
142
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 ))
113
148
commands .append ('sudo rm -rf %s' % (remote_component_tmp_path ))
114
149
deployer_utils .exec_ssh (target_host , root_user , key_file , commands )
115
150
116
151
undo_commands = []
117
- undo_commands .append ('sudo initctl stop %s \n ' % service_name )
152
+ undo_commands .append ('sudo service %s stop \n ' % service_name )
118
153
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 )
120
155
logging .debug ("uninstall commands: %s" , undo_commands )
121
156
122
157
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 )
124
161
logging .debug ("start commands: %s" , start_commands )
125
162
126
163
stop_commands = []
127
- stop_commands .append ('sudo initctl stop %s \n ' % service_name )
164
+ stop_commands .append ('sudo service %s stop \n ' % service_name )
128
165
logging .debug ("stop commands: %s" , stop_commands )
129
166
130
167
return {'ssh' : undo_commands , 'start_cmds' : start_commands , 'stop_cmds' : stop_commands }
0 commit comments