Browse Source

fixed file naming; added build_client method

master
Robin Thoni 8 years ago
parent
commit
280adf04bd
1 changed files with 30 additions and 24 deletions
  1. 30
    24
      vpngen.py

+ 30
- 24
vpngen.py View File

82
 
82
 
83
     def get_client_dir(self, vpn_name, client_name):
83
     def get_client_dir(self, vpn_name, client_name):
84
         base_dir = self.get_base_dir(vpn_name)
84
         base_dir = self.get_base_dir(vpn_name)
85
-        return "%sclients%s%s%s" % (base_dir, os.sep, client_name, os.sep)
85
+        return "%sclients%s%s-%s%s" % (base_dir, os.sep, client_name, vpn_name, os.sep)
86
 
86
 
87
     def get_client_config_path(self, vpn_name, client_name):
87
     def get_client_config_path(self, vpn_name, client_name):
88
         client_dir = self.get_client_dir(vpn_name, client_name)
88
         client_dir = self.get_client_dir(vpn_name, client_name)
198
         if os.path.exists(client_dir):
198
         if os.path.exists(client_dir):
199
             return VpnGenError.ClientAlreadyExists
199
             return VpnGenError.ClientAlreadyExists
200
 
200
 
201
-        client_default_config_path = self.get_client_default_config_path(vpn_name)
202
-        with open(client_default_config_path, "r") as f:
203
-            client_default_config = f.read()
204
-
205
-        variables['name'] = vpn_name
206
-        variables['client'] = client_name
207
-        for variable in variables:
208
-            client_default_config = client_default_config.replace("${%s}" % variable, variables[variable])
209
-
210
-        os.makedirs(client_dir)
211
-        with open(client_conf_file, "w") as f:
212
-            f.write(client_default_config)
213
-
214
         curdir = os.curdir
201
         curdir = os.curdir
215
         easyrsadir = self.get_easy_rsa_dir(vpn_name)
202
         easyrsadir = self.get_easy_rsa_dir(vpn_name)
216
         pkitool = self.get_pkitool_path(vpn_name)
203
         pkitool = self.get_pkitool_path(vpn_name)
224
 
211
 
225
         os.chdir(curdir)
212
         os.chdir(curdir)
226
 
213
 
227
-        files_paths = self.get_all_needed_files_paths(vpn_name, client_name)
228
-        for file_path in files_paths:
229
-            split = os.path.splitext(file_path)
230
-            dest = "%s%s-%s%s" % (client_dir, os.path.basename(split[0]), vpn_name, split[1])
231
-            shutil.copy(file_path, dest)
232
-
233
-        files_names = list(map(lambda file_path: os.path.basename(file_path), files_paths))
234
-
235
-        call(["tar", "cfj", self.get_client_tarball_path(vpn_name, client_name),
236
-              "-C", self.get_easy_rsa_key_dir(vpn_name)] + files_names)
214
+        self.build_client(vpn_name, client_name, variables)
237
 
215
 
238
         return VpnGenError.Success
216
         return VpnGenError.Success
239
 
217
 
249
     def rebuild_clients(self, vpn_name):
227
     def rebuild_clients(self, vpn_name):
250
         base_dir = self.get_base_dir(vpn_name)
228
         base_dir = self.get_base_dir(vpn_name)
251
         return VpnGenError.Success
229
         return VpnGenError.Success
230
+
231
+    def build_client(self, vpn_name, client_name, variables):
232
+        client_dir = self.get_client_dir(vpn_name, client_name)
233
+        client_conf_file = self.get_client_config_path(vpn_name, client_name)
234
+        client_default_config_path = self.get_client_default_config_path(vpn_name)
235
+
236
+        with open(client_default_config_path, "r") as f:
237
+            client_default_config = f.read()
238
+
239
+        variables['name'] = vpn_name
240
+        variables['client'] = client_name
241
+        for variable in variables:
242
+            client_default_config = client_default_config.replace("${%s}" % variable, variables[variable])
243
+
244
+        os.makedirs(client_dir)
245
+        with open(client_conf_file, "w") as f:
246
+            f.write(client_default_config)
247
+
248
+        files_paths = self.get_all_needed_files_paths(vpn_name, client_name)
249
+        for file_path in files_paths:
250
+            split = os.path.splitext(file_path)
251
+            dest = "%s%s-%s%s" % (client_dir, os.path.basename(split[0]), vpn_name, split[1])
252
+            shutil.copy(file_path, dest)
253
+
254
+        files_names = list(map(lambda file_path: os.path.basename(file_path), files_paths))
255
+
256
+        call(["tar", "cfj", self.get_client_tarball_path(vpn_name, client_name),
257
+              "-C", self.get_easy_rsa_key_dir(vpn_name)] + files_names)

Loading…
Cancel
Save