forked from canonical/snapd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclick.go
628 lines (542 loc) · 18.2 KB
/
click.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
// -*- Mode: Go; indent-tabs-mode: t -*-
/*
* Copyright (C) 2014-2015 Canonical Ltd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package snappy
/* This part of the code implements enough of the click file format
to install a "snap" package
Limitations:
- no per-user registration
- no user-level hooks
- more(?)
*/
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"reflect"
"regexp"
"strings"
"text/template"
"time"
"github.com/ubuntu-core/snappy/arch"
"github.com/ubuntu-core/snappy/dirs"
"github.com/ubuntu-core/snappy/helpers"
"github.com/ubuntu-core/snappy/logger"
"github.com/ubuntu-core/snappy/pkg"
"github.com/ubuntu-core/snappy/progress"
"github.com/ubuntu-core/snappy/systemd"
)
type clickManifest struct {
Name string `json:"name"`
Version string `json:"version"`
Architecture []string `json:"architecture,omitempty"`
Type pkg.Type `json:"type,omitempty"`
Framework string `json:"framework,omitempty"`
Description string `json:"description,omitempty"`
Icon string `json:"icon,omitempty"`
InstalledSize string `json:"installed-size,omitempty"`
Maintainer string `json:"maintainer,omitempty"`
Title string `json:"title,omitempty"`
Hooks map[string]clickAppHook `json:"hooks,omitempty"`
}
// wait this time between TERM and KILL
var killWait = 5 * time.Second
// servicesBinariesStringsWhitelist is the whitelist of legal chars
// in the "binaries" and "services" section of the package.yaml
var servicesBinariesStringsWhitelist = regexp.MustCompile(`^[A-Za-z0-9/. _#:-]*$`)
// Execute the hook.Exec command
func execHook(execCmd string) (err error) {
// the spec says this is passed to the shell
cmd := exec.Command("sh", "-c", execCmd)
if output, err := cmd.CombinedOutput(); err != nil {
if exitCode, err := helpers.ExitCode(err); err == nil {
return &ErrHookFailed{
Cmd: execCmd,
Output: string(output),
ExitCode: exitCode,
}
}
return err
}
return nil
}
func readClickManifest(data []byte) (manifest clickManifest, err error) {
r := bytes.NewReader(data)
dec := json.NewDecoder(r)
err = dec.Decode(&manifest)
return manifest, err
}
func readClickManifestFromClickDir(clickDir string) (manifest clickManifest, err error) {
manifestFiles, err := filepath.Glob(filepath.Join(clickDir, ".click", "info", "*.manifest"))
if err != nil {
return manifest, err
}
if len(manifestFiles) != 1 {
return manifest, fmt.Errorf("Error: got %v manifests in %v", len(manifestFiles), clickDir)
}
manifestData, err := ioutil.ReadFile(manifestFiles[0])
manifest, err = readClickManifest([]byte(manifestData))
return manifest, err
}
// generate the name
func generateBinaryName(m *packageYaml, binary Binary) string {
var binName string
if m.Type == pkg.TypeFramework {
binName = filepath.Base(binary.Name)
} else {
binName = fmt.Sprintf("%s.%s", m.Name, filepath.Base(binary.Name))
}
return filepath.Join(dirs.SnapBinariesDir, binName)
}
func binPathForBinary(pkgPath string, binary Binary) string {
return filepath.Join(pkgPath, binary.Exec)
}
func verifyBinariesYaml(binary Binary) error {
return verifyStructStringsAgainstWhitelist(binary, servicesBinariesStringsWhitelist)
}
// Doesn't need to handle complications like internal quotes, just needs to
// wrap right side of an env variable declaration with quotes for the shell.
func quoteEnvVar(envVar string) string {
return "export " + strings.Replace(envVar, "=", "=\"", 1) + "\""
}
func generateSnapBinaryWrapper(binary Binary, pkgPath, aaProfile string, m *packageYaml) (string, error) {
wrapperTemplate := `#!/bin/sh
set -e
# app info (deprecated)
{{.OldAppVars}}
export SNAPP_OLD_PWD="$(pwd)"
# app info
{{.NewAppVars}}
if [ ! -d "$SNAP_APP_USER_DATA_PATH" ]; then
mkdir -p "$SNAP_APP_USER_DATA_PATH"
fi
export HOME="$SNAP_APP_USER_DATA_PATH"
# export old pwd
export SNAP_OLD_PWD="$(pwd)"
cd {{.AppPath}}
ubuntu-core-launcher {{.UdevAppName}} {{.AaProfile}} {{.Target}} "$@"
`
// it's fine for this to error out; we might be in a framework or sth
origin := originFromBasedir(pkgPath)
if err := verifyBinariesYaml(binary); err != nil {
return "", err
}
actualBinPath := binPathForBinary(pkgPath, binary)
udevPartName := m.qualifiedName(origin)
var templateOut bytes.Buffer
t := template.Must(template.New("wrapper").Parse(wrapperTemplate))
wrapperData := struct {
AppName string
AppArch string
AppPath string
Version string
UdevAppName string
Origin string
Home string
Target string
AaProfile string
OldAppVars string
NewAppVars string
}{
AppName: m.Name,
AppArch: arch.UbuntuArchitecture(),
AppPath: pkgPath,
Version: m.Version,
UdevAppName: udevPartName,
Origin: origin,
Home: "$HOME",
Target: actualBinPath,
AaProfile: aaProfile,
}
oldVars := []string{}
for _, envVar := range append(
helpers.GetDeprecatedBasicSnapEnvVars(wrapperData),
helpers.GetDeprecatedUserSnapEnvVars(wrapperData)...) {
oldVars = append(oldVars, quoteEnvVar(envVar))
}
wrapperData.OldAppVars = strings.Join(oldVars, "\n")
newVars := []string{}
for _, envVar := range append(
helpers.GetBasicSnapEnvVars(wrapperData),
helpers.GetUserSnapEnvVars(wrapperData)...) {
newVars = append(newVars, quoteEnvVar(envVar))
}
wrapperData.NewAppVars = strings.Join(newVars, "\n")
t.Execute(&templateOut, wrapperData)
return templateOut.String(), nil
}
// verifyStructStringsAgainstWhitelist takes a struct and ensures that
// the given whitelist regexp matches all string fields of the struct
func verifyStructStringsAgainstWhitelist(s interface{}, whitelist *regexp.Regexp) error {
// check all members of the services struct against our whitelist
t := reflect.TypeOf(s)
v := reflect.ValueOf(s)
for i := 0; i < t.NumField(); i++ {
// PkgPath means its a unexported field and we can ignore it
if t.Field(i).PkgPath != "" {
continue
}
if v.Field(i).Kind() == reflect.Ptr {
vi := v.Field(i).Elem()
if vi.Kind() == reflect.Struct {
return verifyStructStringsAgainstWhitelist(vi.Interface(), whitelist)
}
}
if v.Field(i).Kind() == reflect.Struct {
vi := v.Field(i).Interface()
return verifyStructStringsAgainstWhitelist(vi, whitelist)
}
if v.Field(i).Kind() == reflect.String {
key := t.Field(i).Name
value := v.Field(i).String()
if !whitelist.MatchString(value) {
return &ErrStructIllegalContent{
Field: key,
Content: value,
Whitelist: whitelist.String(),
}
}
}
}
return nil
}
func verifyServiceYaml(service ServiceYaml) error {
return verifyStructStringsAgainstWhitelist(service, servicesBinariesStringsWhitelist)
}
func generateSnapServicesFile(service ServiceYaml, baseDir string, aaProfile string, m *packageYaml) (string, error) {
if err := verifyServiceYaml(service); err != nil {
return "", err
}
udevPartName := m.qualifiedName(originFromBasedir(baseDir))
desc := service.Description
if desc == "" {
desc = fmt.Sprintf("service %s for package %s", service.Name, m.Name)
}
socketFileName := ""
if service.Socket {
socketFileName = filepath.Base(generateSocketFileName(m, service))
}
return systemd.New(dirs.GlobalRootDir, nil).GenServiceFile(
&systemd.ServiceDescription{
AppName: m.Name,
ServiceName: service.Name,
Version: m.Version,
Description: desc,
AppPath: baseDir,
Start: service.Start,
Stop: service.Stop,
PostStop: service.PostStop,
StopTimeout: time.Duration(service.StopTimeout),
AaProfile: aaProfile,
IsFramework: m.Type == pkg.TypeFramework,
IsNetworked: service.Ports != nil && len(service.Ports.External) > 0,
BusName: service.BusName,
Forking: service.Forking,
UdevAppName: udevPartName,
Socket: service.Socket,
SocketFileName: socketFileName,
}), nil
}
func generateSnapSocketFile(service ServiceYaml, baseDir string, aaProfile string, m *packageYaml) (string, error) {
if err := verifyServiceYaml(service); err != nil {
return "", err
}
// lp: #1515709, systemd will default to 0666 if no socket mode
// is specified
if service.SocketMode == "" {
service.SocketMode = "0660"
}
serviceFileName := filepath.Base(generateServiceFileName(m, service))
return systemd.New(dirs.GlobalRootDir, nil).GenSocketFile(
&systemd.ServiceDescription{
ServiceFileName: serviceFileName,
ListenStream: service.ListenStream,
SocketMode: service.SocketMode,
SocketUser: service.SocketUser,
SocketGroup: service.SocketGroup,
}), nil
}
func generateServiceFileName(m *packageYaml, service ServiceYaml) string {
return filepath.Join(dirs.SnapServicesDir, fmt.Sprintf("%s_%s_%s.service", m.Name, service.Name, m.Version))
}
func generateSocketFileName(m *packageYaml, service ServiceYaml) string {
return filepath.Join(dirs.SnapServicesDir, fmt.Sprintf("%s_%s_%s.socket", m.Name, service.Name, m.Version))
}
func generateBusPolicyFileName(m *packageYaml, service ServiceYaml) string {
return filepath.Join(dirs.SnapBusPolicyDir, fmt.Sprintf("%s_%s_%s.conf", m.Name, service.Name, m.Version))
}
// takes a directory and removes the global root, this is needed
// when the SetRoot option is used and we need to generate
// content for the "ServiceYamls" and "Binaries" section
var stripGlobalRootDir = stripGlobalRootDirImpl
func stripGlobalRootDirImpl(dir string) string {
if dirs.GlobalRootDir == "/" {
return dir
}
return dir[len(dirs.GlobalRootDir):]
}
func (m *packageYaml) addPackageServices(baseDir string, inhibitHooks bool, inter interacter) error {
for _, service := range m.ServiceYamls {
aaProfile, err := getSecurityProfile(m, service.Name, baseDir)
if err != nil {
return err
}
// this will remove the global base dir when generating the
// service file, this ensures that /apps/foo/1.0/bin/start
// is in the service file when the SetRoot() option
// is used
realBaseDir := stripGlobalRootDir(baseDir)
// Generate service file
content, err := generateSnapServicesFile(service, realBaseDir, aaProfile, m)
if err != nil {
return err
}
serviceFilename := generateServiceFileName(m, service)
os.MkdirAll(filepath.Dir(serviceFilename), 0755)
if err := helpers.AtomicWriteFile(serviceFilename, []byte(content), 0644, 0); err != nil {
return err
}
// Generate systemd socket file if needed
if service.Socket {
content, err := generateSnapSocketFile(service, realBaseDir, aaProfile, m)
if err != nil {
return err
}
socketFilename := generateSocketFileName(m, service)
os.MkdirAll(filepath.Dir(socketFilename), 0755)
if err := helpers.AtomicWriteFile(socketFilename, []byte(content), 0644, 0); err != nil {
return err
}
}
// If necessary, generate the DBus policy file so the framework
// service is allowed to start
if m.Type == pkg.TypeFramework && service.BusName != "" {
content, err := genBusPolicyFile(service.BusName)
if err != nil {
return err
}
policyFilename := generateBusPolicyFileName(m, service)
os.MkdirAll(filepath.Dir(policyFilename), 0755)
if err := helpers.AtomicWriteFile(policyFilename, []byte(content), 0644, 0); err != nil {
return err
}
}
// daemon-reload and start only if we are not in the
// inhibitHooks mode
//
// *but* always run enable (which just sets a symlink)
serviceName := filepath.Base(generateServiceFileName(m, service))
sysd := systemd.New(dirs.GlobalRootDir, inter)
if !inhibitHooks {
if err := sysd.DaemonReload(); err != nil {
return err
}
}
// we always enable the service even in inhibit hooks
if err := sysd.Enable(serviceName); err != nil {
return err
}
if !inhibitHooks {
if err := sysd.Start(serviceName); err != nil {
return err
}
}
if service.Socket {
socketName := filepath.Base(generateSocketFileName(m, service))
// we always enable the socket even in inhibit hooks
if err := sysd.Enable(socketName); err != nil {
return err
}
if !inhibitHooks {
if err := sysd.Start(socketName); err != nil {
return err
}
}
}
}
return nil
}
func (m *packageYaml) removePackageServices(baseDir string, inter interacter) error {
sysd := systemd.New(dirs.GlobalRootDir, inter)
for _, service := range m.ServiceYamls {
serviceName := filepath.Base(generateServiceFileName(m, service))
if err := sysd.Disable(serviceName); err != nil {
return err
}
if err := sysd.Stop(serviceName, time.Duration(service.StopTimeout)); err != nil {
if !systemd.IsTimeout(err) {
return err
}
inter.Notify(fmt.Sprintf("%s refused to stop, killing.", serviceName))
// ignore errors for kill; nothing we'd do differently at this point
sysd.Kill(serviceName, "TERM")
time.Sleep(killWait)
sysd.Kill(serviceName, "KILL")
}
if err := os.Remove(generateServiceFileName(m, service)); err != nil && !os.IsNotExist(err) {
logger.Noticef("Failed to remove service file for %q: %v", serviceName, err)
}
if err := os.Remove(generateSocketFileName(m, service)); err != nil && !os.IsNotExist(err) {
logger.Noticef("Failed to remove socket file for %q: %v", serviceName, err)
}
// Also remove DBus system policy file
if err := os.Remove(generateBusPolicyFileName(m, service)); err != nil && !os.IsNotExist(err) {
logger.Noticef("Failed to remove bus policy file for service %q: %v", serviceName, err)
}
}
// only reload if we actually had services
if len(m.ServiceYamls) > 0 {
if err := sysd.DaemonReload(); err != nil {
return err
}
}
return nil
}
func (m *packageYaml) addPackageBinaries(baseDir string) error {
if err := os.MkdirAll(dirs.SnapBinariesDir, 0755); err != nil {
return err
}
for _, binary := range m.Binaries {
aaProfile, err := getSecurityProfile(m, binary.Name, baseDir)
if err != nil {
return err
}
// this will remove the global base dir when generating the
// service file, this ensures that /apps/foo/1.0/bin/start
// is in the service file when the SetRoot() option
// is used
realBaseDir := stripGlobalRootDir(baseDir)
content, err := generateSnapBinaryWrapper(binary, realBaseDir, aaProfile, m)
if err != nil {
return err
}
if err := helpers.AtomicWriteFile(generateBinaryName(m, binary), []byte(content), 0755, 0); err != nil {
return err
}
}
return nil
}
func (m *packageYaml) removePackageBinaries(baseDir string) error {
for _, binary := range m.Binaries {
os.Remove(generateBinaryName(m, binary))
}
return nil
}
type agreer interface {
Agreed(intro, license string) bool
}
type interacter interface {
agreer
Notify(status string)
}
// this rewrites the json manifest to include the origin in the on-disk
// manifest.json to be compatible with click again
func writeCompatManifestJSON(clickMetaDir string, manifestData []byte, origin string) error {
var cm clickManifest
if err := json.Unmarshal(manifestData, &cm); err != nil {
return err
}
if cm.Type != pkg.TypeFramework && cm.Type != pkg.TypeGadget {
// add the origin to the name
cm.Name = fmt.Sprintf("%s.%s", cm.Name, origin)
}
if origin == SideloadedOrigin {
cm.Version = filepath.Base(filepath.Join(clickMetaDir, "..", ".."))
}
outStr, err := json.MarshalIndent(cm, "", " ")
if err != nil {
return err
}
if err := helpers.AtomicWriteFile(filepath.Join(clickMetaDir, cm.Name+".manifest"), []byte(outStr), 0644, 0); err != nil {
return err
}
return nil
}
func installClick(snapFile string, flags InstallFlags, inter progress.Meter, origin string) (name string, err error) {
allowUnauthenticated := (flags & AllowUnauthenticated) != 0
part, err := NewSnapPartFromSnapFile(snapFile, origin, allowUnauthenticated)
if err != nil {
return "", err
}
defer part.deb.Close()
return part.Install(inter, flags)
}
// removeSnapData removes the data for the given version of the given snap
func removeSnapData(fullName, version string) error {
dirs, err := snapDataDirs(fullName, version)
if err != nil {
return err
}
for _, dir := range dirs {
if err := os.RemoveAll(dir); err != nil && !os.IsNotExist(err) {
return err
}
os.Remove(filepath.Dir(dir))
}
return nil
}
// snapDataDirs returns the list of data directories for the given snap version
func snapDataDirs(fullName, version string) ([]string, error) {
// collect the directories, homes first
found, err := filepath.Glob(filepath.Join(dirs.SnapDataHomeGlob, fullName, version))
if err != nil {
return nil, err
}
// then system data
systemPath := filepath.Join(dirs.SnapDataDir, fullName, version)
found = append(found, systemPath)
return found, nil
}
// Copy all data for "fullName" from "oldVersion" to "newVersion"
// (but never overwrite)
func copySnapData(fullName, oldVersion, newVersion string) (err error) {
oldDataDirs, err := snapDataDirs(fullName, oldVersion)
if err != nil {
return err
}
for _, oldDir := range oldDataDirs {
// replace the trailing "../$old-ver" with the "../$new-ver"
newDir := filepath.Join(filepath.Dir(oldDir), newVersion)
if err := copySnapDataDirectory(oldDir, newDir); err != nil {
return err
}
}
return nil
}
// Lowlevel copy the snap data (but never override existing data)
func copySnapDataDirectory(oldPath, newPath string) (err error) {
if _, err := os.Stat(oldPath); err == nil {
if _, err := os.Stat(newPath); err != nil {
// there is no golang "CopyFile"
cmd := exec.Command("cp", "-a", oldPath, newPath)
if err := cmd.Run(); err != nil {
if exitCode, err := helpers.ExitCode(err); err == nil {
return &ErrDataCopyFailed{
OldPath: oldPath,
NewPath: newPath,
ExitCode: exitCode}
}
return err
}
}
}
return nil
}