controller.nim 6.5 KB
Newer Older
Nong Hoang Tu's avatar
Nong Hoang Tu committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#[
  Description:
    Control workflow of update, clone and pull.
  Authors: 2020, Nong Hoang Tu <dmknght@parrotsec.org>
  Contributors:
  Module structure:
    - doUpdateProject: Control the way a project is updated
    - updateProjectAll: Call doUpdateProject for all projects in root_folder
    - updateProjectArr: Call doUpdateProject, update projects that were given by user
    - pullProjectAll: Pull all projects in root_folder
    - pullProjectArr: Pull projects were given by user
    - cloneProject: Clone projects were given by user
    - handleUpdateProject: Select updateProjectArr or updateProjectAll by
      number of projects were given by user. This proc is called from main
    - handlePullProject: Select updatePulltArr or updatePullAll by
      number of projects were given by user. This proc is called from main
  TODO:
]#


Nong Hoang Tu's avatar
Nong Hoang Tu committed
21
import configs
22
import .. / interfaces / logutils
23
import .. / update / cmdutils
Nong Hoang Tu's avatar
Nong Hoang Tu committed
24
import os
25
26


27
proc doUpdateProject(config: ProgConf, project: DebPackage): Err =
Nong Hoang Tu's avatar
Nong Hoang Tu committed
28
29
30
31
32
33
34
35
36
  #[
    Workflow:
      1. Pull project to sync changes from manual packaging
      2. Check for update
      3. Upgrade source code
      4. Create changelog
      5. Create tag
      6. Push
  ]#
Nong Hoang Tu's avatar
Nong Hoang Tu committed
37
  var stderr, localVer, serverVer: string
Nong Hoang Tu's avatar
Nong Hoang Tu committed
38
39
40
41

  if gbpPull(config.uscanPath, project.path, stderr) != 0:
    project.log.logErr(project.name, "Error while pulling", stderr)
    return Err.failed
Nong Hoang Tu's avatar
Nong Hoang Tu committed
42
43
  else:
    project.log.logInfo(project.name, "Pull completed. Start checking new update")
Nong Hoang Tu's avatar
Nong Hoang Tu committed
44
45

  let updateStt = checkUpdate(config.uscanPath, project.path, localVer, serverVer, stderr)
46
47
  if updateStt == Err.updated:
    project.log.logInfo(project.name, "Up to date")
Nong Hoang Tu's avatar
Nong Hoang Tu committed
48
    return Err.updated
Nong Hoang Tu's avatar
Nong Hoang Tu committed
49
  elif updateStt == Err.failed:
Nong Hoang Tu's avatar
Nong Hoang Tu committed
50
51
    project.log.logErr(project.name, "Error while checking for new version", stderr)
    return Err.updated
Nong Hoang Tu's avatar
Nong Hoang Tu committed
52
53
  else:
    project.log.logInfo(project.name, "[" & localVer & "] [" & serverVer & "]. Start upgrading source code.")
Nong Hoang Tu's avatar
Nong Hoang Tu committed
54
55
56
57

  if gbpUpdate(config.uscanPath, project.path, stderr) != 0:
    project.log.logErr(project.name, "Error while updating", stderr)
    return Err.failed
Nong Hoang Tu's avatar
Nong Hoang Tu committed
58
59
  else:
    project.log.logInfo(project.name, "Upgrade to new version successfully. Making changelog.")
Nong Hoang Tu's avatar
Nong Hoang Tu committed
60
61
62
63

  if gbpDch(config.uscanPath, project.path, serverVer, stderr) != 0:
    project.log.logErr(project.name, "Error while creating new changelog", stderr)
    return Err.failed
Nong Hoang Tu's avatar
Nong Hoang Tu committed
64
65
  else:
    project.log.logInfo(project.name, "New changelog was created. Making new tag.")
Nong Hoang Tu's avatar
Nong Hoang Tu committed
66
67
68
69
  
  if gbpTag(config.uscanPath, project.path, stderr) != 0:
    project.log.logErr(project.name, "Error while creating new tag", stderr)
    return Err.failed
Nong Hoang Tu's avatar
Nong Hoang Tu committed
70
71
  else:
    project.log.logInfo(project.name, "Create new tag successfully. Start pushing.")
Nong Hoang Tu's avatar
Nong Hoang Tu committed
72
73
74
75

  if gbpPush(config.uscanPath, project.path, stderr) != 0:
    project.log.logErr(project.name, "Error while pushing new patches", stderr)
    return Err.failed
Nong Hoang Tu's avatar
Nong Hoang Tu committed
76
77
78
  else:
    project.log.logInfo(project.name, "Pushed new version to " & dirName(config.gitUrl) & project.name)
    return Err.updateSuccess
79
80


Nong Hoang Tu's avatar
Nong Hoang Tu committed
81
82
83
84
85
86
87
88
89
90
proc updateProjectAll(config: ProgConf) =
  let
    resultLog = initResultLogs(config.logPath)
    fullLog = initFullLogs(config.logPath)
  var project: DebPackage
  project.log = fullLog
  for kind, path in walkDir(config.rootPath):
    if kind == pcDir:
      project.path = path
      project.name = path.splitPath.tail
91
92
93
      let updateResult = doUpdateProject(config, project)
      if updateResult == Err.updated:
        resultLog.logInfo(project.name, "Up to date")
Nong Hoang Tu's avatar
Nong Hoang Tu committed
94
95
96
97
      elif updateResult == Err.updateSuccess:
        resultLog.logInfo(project.name, "Update successfully")
      else:
        resultLog.logErr(project.name, "Failed to update", "")
Nong Hoang Tu's avatar
Nong Hoang Tu committed
98
99


100
101
102
103
104
105
106
107
proc updateProjectArr(config: ProgConf, projectArr: seq[string]) =
  let
    resultLog = initResultLogs(config.logPath)
    fullLog = initFullLogs(config.logPath)
  var project: DebPackage
  project.log = fullLog
  for name in projectArr:
    project.name = name
Nong Hoang Tu's avatar
Nong Hoang Tu committed
108
    project.path = dirName(config.rootPath & name)
109
110
111
112
113
114
115
116
117
118
    if not dirExists(project.path):
      resultLog.logErr(project.name, "Dir doesn't exist", "")
    else:
      let updateResult = doUpdateProject(config, project)
      if updateResult == Err.updated:
        resultLog.logInfo(project.name, "Up to date")
      elif updateResult == Err.updateSuccess:
        resultLog.logInfo(project.name, "Update successfully")
      else:
        resultLog.logErr(project.name, "Failed to update", "")
119
120


Nong Hoang Tu's avatar
Nong Hoang Tu committed
121
proc pullProjectAll(config: ProgConf) =
Nong Hoang Tu's avatar
Nong Hoang Tu committed
122
123
124
125
126
127
128
129
130
131
132
133
  let
    fullLog = initFullLogs(config.logPath)
  var
    project: DebPackage
    stderr: string
  project.log = fullLog
  for kind, path in walkDir(config.rootPath):
    if kind == pcDir:
      project.path = path
      project.name = path.splitPath.tail
    if gbpPull(config.uscanPath, project.path, stderr) != 0:
      project.log.logErr(project.name, "Error while pulling", stderr)
Nong Hoang Tu's avatar
Nong Hoang Tu committed
134
135
    else:
      project.log.logInfo(project.name, "Pulled successfully.")
136

Nong Hoang Tu's avatar
Nong Hoang Tu committed
137

Nong Hoang Tu's avatar
Nong Hoang Tu committed
138
139
140
141
142
143
144
145
146
proc pullProjectArr(config: ProgConf, projectArr: seq[string]) =
  let
    fullLog = initFullLogs(config.logPath)
  var
    project: DebPackage
    stderr: string
  project.log = fullLog
  for name in projectArr:
    project.name = name
Nong Hoang Tu's avatar
Nong Hoang Tu committed
147
    project.path = dirName(config.rootPath & name)
Nong Hoang Tu's avatar
Nong Hoang Tu committed
148
149
150
151
152
    if not dirExists(project.path):
      project.log.logErr(project.name, "Dir doesn't exist", "")
    else:
      if gbpPull(config.uscanPath, project.path, stderr) != 0:
        project.log.logErr(project.name, "Error while pulling", stderr)
Nong Hoang Tu's avatar
Nong Hoang Tu committed
153
154
      else:
        project.log.logInfo(project.name, "Pulled successfully.")
155

Nong Hoang Tu's avatar
Nong Hoang Tu committed
156

Nong Hoang Tu's avatar
Nong Hoang Tu committed
157
proc cloneProject*(projects: seq[string]) =
Nong Hoang Tu's avatar
Nong Hoang Tu committed
158
  var
Nong Hoang Tu's avatar
Nong Hoang Tu committed
159
    config: ProgConf
Nong Hoang Tu's avatar
Nong Hoang Tu committed
160
161
    project: DebPackage
    stderr: string
Nong Hoang Tu's avatar
Nong Hoang Tu committed
162
163
164
165
166
167
  let
    fullLog = initFullLogs(config.logPath)
  
  if not initConfig(config):
    return
  
Nong Hoang Tu's avatar
Nong Hoang Tu committed
168
169
170
171
172
173
174
175
176
177
178
  project.log = fullLog
  for name in projects:
    project.name = name
    project.path = dirName(config.rootPath & name)
    if dirExists(project.path):
      project.log.logErr(project.name, "Dir exists", "")
    else:
      if gbpClone(config.gbpPath, config.gitUrl, project.name, project.path, stderr) != 0:
        project.log.logErr(project.name, "Error while cloning", stderr)
      else:
        project.log.logInfo(project.name, "Cloned successfully.")
179

Nong Hoang Tu's avatar
Nong Hoang Tu committed
180

Nong Hoang Tu's avatar
Nong Hoang Tu committed
181
182
183
184
185
186
187
proc handleUpdateProject*(projects: seq[string]) =
  var config: ProgConf
  if not initConfig(config):
    return
  if len(projects) == 0:
    updateProjectAll(config)
  else:
188
    updateProjectArr(config, projects)
189

Nong Hoang Tu's avatar
Nong Hoang Tu committed
190

Nong Hoang Tu's avatar
Nong Hoang Tu committed
191
192
193
194
195
proc handlePullProject*(projects: seq[string]) =
  var config: ProgConf
  if not initConfig(config):
    return
  if len(projects) == 0:
196
    pullProjectAll(config)
Nong Hoang Tu's avatar
Nong Hoang Tu committed
197
  else:
Nong Hoang Tu's avatar
Nong Hoang Tu committed
198
    pullProjectArr(config, projects)