utils.py 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import os
  2. import json
  3. import glob
  4. def fetch_param_file_path():
  5. param_file_path = '/home/jovyan/EX-WORKFLOW/param_files/params.json'
  6. return param_file_path
  7. def fetch_ssh_config_path():
  8. ssh_config_path = '/home/jovyan/.ssh/config'
  9. return ssh_config_path
  10. def config_mdx(name_mdx, mdxDomain):
  11. # mdx接続情報を設定ファイルに反映させる
  12. path = fetch_ssh_config_path()
  13. s = ''
  14. if os.path.exists(path):
  15. # 設定ファイルがある場合
  16. with open(path, "r") as f:
  17. s = f.read()
  18. # mdxの設定があれば該当部分のみ削除して設定を新たに追記する
  19. if s.find('Host mdx') == -1:
  20. # mdxの設定が無ければ追記する
  21. write_mdx_config(mode='a', mdxDomain=mdxDomain, name_mdx=name_mdx)
  22. else:
  23. #mdxの設定があれば該当部分のみ削除して設定を新たに追記する
  24. front = s[:s.find('Host mdx')]
  25. front = front.rstrip()
  26. find_words = 'IdentityFile ~/.ssh/id_rsa\n\tStrictHostKeyChecking no'
  27. back = s[(s.find(find_words) + len(find_words)):]
  28. back = back.strip()
  29. if len(back) >= 1:
  30. s = front + '\n' + back + '\n'
  31. elif len(front) <= 0:
  32. s = front
  33. else:
  34. s = front + '\n'
  35. with open(path, 'w') as f:
  36. f.write(s)
  37. write_mdx_config(mode='a', mdxDomain=mdxDomain, name_mdx=name_mdx)
  38. else:
  39. # 設定ファイルが無い場合、新規作成して新たに書き込む
  40. write_mdx_config(mode='w', mdxDomain=mdxDomain, name_mdx=name_mdx)
  41. def write_mdx_config(mode, mdxDomain, name_mdx):
  42. path = fetch_ssh_config_path()
  43. with open(path, mode) as f:
  44. f.write('\nHost mdx\n')
  45. f.write('\tHostname ' + mdxDomain + '\n')
  46. f.write('\tUser ' + name_mdx + '\n')
  47. f.write('\tPort 22\n')
  48. f.write('\tIdentityFile ~/.ssh/id_rsa\n')
  49. f.write('\tStrictHostKeyChecking no\n')
  50. def config_GIN(ginHttp):
  51. # SSHホスト(=GIN)を信頼する設定
  52. path = fetch_ssh_config_path()
  53. s=''
  54. ginDomain = ginHttp.split('/')[-2]
  55. if os.path.exists(path):
  56. with open(path, 'r') as f:
  57. s = f.read()
  58. if s.find('host ' + ginDomain +'\n\tStrictHostKeyChecking no\n\tUserKnownHostsFile=/dev/null') == -1:
  59. # 設定が無い場合は追記する
  60. with open('/home/jovyan/.ssh/config', mode='a') as f:
  61. write_GIN_config(mode='a', ginDomain = ginDomain)
  62. else:
  63. # すでにGINを信頼する設定があれば何もしない
  64. pass
  65. else:
  66. # 設定ファイルが無い場合は新規作成して設定を書きこむ
  67. with open('/home/jovyan/.ssh/config', mode='w') as f:
  68. write_GIN_config(mode='w', ginDomain = ginDomain)
  69. def write_GIN_config(mode, ginDomain):
  70. path = fetch_ssh_config_path()
  71. with open(path, mode) as f:
  72. f.write('\nhost ' + ginDomain +'\n')
  73. f.write('\tStrictHostKeyChecking no\n')
  74. f.write('\tUserKnownHostsFile=/dev/null\n')
  75. def fetch_files(dir_path):
  76. """引数に与えたディレクトリパス以下にあるファイルのリストを作成して返す"""
  77. data_list = []
  78. files = glob.glob(dir_path + "/*")
  79. for f in files:
  80. data_list += [f]
  81. return data_list