controller.nim 7.4 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

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

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

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

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

Nong Hoang Tu's avatar
Nong Hoang Tu committed
78
  project.log.logDebug(project.name, "Pushing branches at " & project.path)
79
  if gbpPush(config.gbpPath, project.path, stderr) != 0:
Nong Hoang Tu's avatar
Nong Hoang Tu committed
80
81
    project.log.logErr(project.name, "Error while pushing new patches", stderr)
    return Err.failed
Nong Hoang Tu's avatar
Nong Hoang Tu committed
82
83
84
  else:
    project.log.logInfo(project.name, "Pushed new version to " & dirName(config.gitUrl) & project.name)
    return Err.updateSuccess
85
86


Nong Hoang Tu's avatar
Nong Hoang Tu committed
87
88
89
90
91
92
93
94
95
96
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
Nong Hoang Tu's avatar
Nong Hoang Tu committed
97
      project.log.logDebug(project.name, "Upgrading at " & project.path)
98
99
100
      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
101
102
103
104
      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
105
106


107
108
109
110
111
112
113
114
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
115
    project.path = dirName(config.rootPath & name)
116
117
118
    if not dirExists(project.path):
      resultLog.logErr(project.name, "Dir doesn't exist", "")
    else:
Nong Hoang Tu's avatar
Nong Hoang Tu committed
119
      project.log.logDebug(project.name, "Upgrading at " & project.path)
120
121
122
123
124
125
126
      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", "")
127
128


Nong Hoang Tu's avatar
Nong Hoang Tu committed
129
proc pullProjectAll(config: ProgConf) =
Nong Hoang Tu's avatar
Nong Hoang Tu committed
130
131
132
133
134
135
136
137
138
139
  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
Nong Hoang Tu's avatar
Nong Hoang Tu committed
140
      project.log.logDebug(project.name, "Pulling at " & project.path)
141
      if gbpPull(config.gbpPath, project.path, stderr) != 0:
Nong Hoang Tu's avatar
Nong Hoang Tu committed
142
143
144
        project.log.logErr(project.name, "Error while pulling", stderr)
      else:
        project.log.logInfo(project.name, "Pulled successfully.")
145

Nong Hoang Tu's avatar
Nong Hoang Tu committed
146

Nong Hoang Tu's avatar
Nong Hoang Tu committed
147
148
149
150
151
152
153
154
155
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
156
    project.path = dirName(config.rootPath & name)
Nong Hoang Tu's avatar
Nong Hoang Tu committed
157
158
159
    if not dirExists(project.path):
      project.log.logErr(project.name, "Dir doesn't exist", "")
    else:
Nong Hoang Tu's avatar
Nong Hoang Tu committed
160
      project.log.logDebug(project.name, "Pulling at " & project.path)
161
      if gbpPull(config.gbpPath, project.path, stderr) != 0:
Nong Hoang Tu's avatar
Nong Hoang Tu committed
162
        project.log.logErr(project.name, "Error while pulling", stderr)
Nong Hoang Tu's avatar
Nong Hoang Tu committed
163
164
      else:
        project.log.logInfo(project.name, "Pulled successfully.")
165

Nong Hoang Tu's avatar
Nong Hoang Tu committed
166

Nong Hoang Tu's avatar
Nong Hoang Tu committed
167
proc cloneProject*(projects: seq[string]) =
Nong Hoang Tu's avatar
Nong Hoang Tu committed
168
  var
Nong Hoang Tu's avatar
Nong Hoang Tu committed
169
    config: ProgConf
170
171
172
173
174

  if not initConfig(config):
    return

  var
Nong Hoang Tu's avatar
Nong Hoang Tu committed
175
176
    project: DebPackage
    stderr: string
Nong Hoang Tu's avatar
Nong Hoang Tu committed
177
178
179
  let
    fullLog = initFullLogs(config.logPath)
  
Nong Hoang Tu's avatar
Nong Hoang Tu committed
180
181
182
183
184
185
186
  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:
Nong Hoang Tu's avatar
Nong Hoang Tu committed
187
      project.log.logDebug(project.name, "Cloning from " & config.gitUrl & project.name & " to " & project.path)
188
      if gbpClone(config.gbpPath, config.gitUrl, project.name, config.rootPath, stderr) != 0:
Nong Hoang Tu's avatar
Nong Hoang Tu committed
189
190
191
        project.log.logErr(project.name, "Error while cloning", stderr)
      else:
        project.log.logInfo(project.name, "Cloned successfully.")
192

Nong Hoang Tu's avatar
Nong Hoang Tu committed
193

Nong Hoang Tu's avatar
Nong Hoang Tu committed
194
195
196
197
198
199
200
proc handleUpdateProject*(projects: seq[string]) =
  var config: ProgConf
  if not initConfig(config):
    return
  if len(projects) == 0:
    updateProjectAll(config)
  else:
201
    updateProjectArr(config, projects)
202

Nong Hoang Tu's avatar
Nong Hoang Tu committed
203

Nong Hoang Tu's avatar
Nong Hoang Tu committed
204
205
206
207
208
proc handlePullProject*(projects: seq[string]) =
  var config: ProgConf
  if not initConfig(config):
    return
  if len(projects) == 0:
209
    pullProjectAll(config)
Nong Hoang Tu's avatar
Nong Hoang Tu committed
210
  else:
Nong Hoang Tu's avatar
Nong Hoang Tu committed
211
    pullProjectArr(config, projects)