~mvo/snappy/snappy-lp1460152-workaround-15.04

« back to all changes in this revision

Viewing changes to snappy/click.go

  • Committer: Snappy Tarmac
  • Author(s): Michael Terry
  • Date: 2015-06-04 10:52:06 UTC
  • mfrom: (446.1.1 envvars-15.04)
  • Revision ID: snappy_tarmac-20150604105206-4vmhtmiysjwy4h7i
Unify handling of app environment variables. Now hooks, binaries, and services all have the same variables (mostly).

This is a backport of r483 from trunk. by mterry approved by chipaca

Show diffs side-by-side

added added

removed removed

Lines of Context:
358
358
        return verifyStructStringsAgainstWhitelist(binary, servicesBinariesStringsWhitelist)
359
359
}
360
360
 
 
361
// Doesn't need to handle complications like internal quotes, just needs to
 
362
// wrap right side of an env variable declaration with quotes for the shell.
 
363
func quoteEnvVar(envVar string) string {
 
364
        return "export " + strings.Replace(envVar, "=", "=\"", 1) + "\""
 
365
}
 
366
 
361
367
func generateSnapBinaryWrapper(binary Binary, pkgPath, aaProfile string, m *packageYaml) (string, error) {
362
368
        wrapperTemplate := `#!/bin/sh
363
369
# !!!never remove this line!!!
365
371
 
366
372
set -e
367
373
 
368
 
TMPDIR="/tmp/snaps/{{.UdevAppName}}/{{.Version}}/tmp"
369
 
if [ ! -d "$TMPDIR" ]; then
370
 
    mkdir -p -m1777 "$TMPDIR"
371
 
fi
372
 
export TMPDIR
373
 
export TEMPDIR="$TMPDIR"
374
 
 
375
 
# app paths (deprecated)
376
 
export SNAPP_APP_PATH="{{.Path}}"
377
 
export SNAPP_APP_DATA_PATH="/var/lib/{{.Path}}"
378
 
export SNAPP_APP_USER_DATA_PATH="$HOME/{{.Path}}"
379
 
export SNAPP_APP_TMPDIR="$TMPDIR"
 
374
# app info (deprecated)
 
375
{{.OldAppVars}}
380
376
export SNAPP_OLD_PWD="$(pwd)"
381
377
 
382
378
# app info
383
 
export SNAP_NAME="{{.Name}}"
384
 
export SNAP_ORIGIN="{{.Namespace}}"
385
 
export SNAP_FULLNAME="{{.UdevAppName}}"
386
 
 
387
 
# app paths
388
 
export SNAP_APP_PATH="{{.Path}}"
389
 
export SNAP_APP_DATA_PATH="/var/lib/{{.Path}}"
390
 
export SNAP_APP_USER_DATA_PATH="$HOME/{{.Path}}"
391
 
export SNAP_APP_TMPDIR="$TMPDIR"
392
 
 
393
 
# FIXME: this will need to become snappy arch or something
394
 
export SNAPPY_APP_ARCH="$(dpkg --print-architecture)"
 
379
{{.NewAppVars}}
 
380
 
 
381
if [ ! -d "$SNAP_APP_TMPDIR" ]; then
 
382
    mkdir -p -m1777 "$SNAP_APP_TMPDIR"
 
383
fi
395
384
 
396
385
if [ ! -d "$SNAP_APP_USER_DATA_PATH" ]; then
397
386
   mkdir -p "$SNAP_APP_USER_DATA_PATH"
400
389
 
401
390
# export old pwd
402
391
export SNAP_OLD_PWD="$(pwd)"
403
 
cd {{.Path}}
 
392
cd {{.AppPath}}
404
393
ubuntu-core-launcher {{.UdevAppName}} {{.AaProfile}} {{.Target}} "$@"
405
394
`
406
395
 
420
409
        var templateOut bytes.Buffer
421
410
        t := template.Must(template.New("wrapper").Parse(wrapperTemplate))
422
411
        wrapperData := struct {
423
 
                Name        string
 
412
                AppName     string
 
413
                AppArch     string
 
414
                AppPath     string
424
415
                Version     string
 
416
                UdevAppName string
 
417
                Namespace   string
 
418
                Home        string
425
419
                Target      string
426
 
                Path        string
427
420
                AaProfile   string
428
 
                UdevAppName string
429
 
                Namespace   string
 
421
                OldAppVars  string
 
422
                NewAppVars  string
430
423
        }{
431
 
                Name:        m.Name,
 
424
                AppName:     m.Name,
 
425
                AppArch:     helpers.UbuntuArchitecture(),
 
426
                AppPath:     pkgPath,
432
427
                Version:     m.Version,
 
428
                UdevAppName: udevPartName,
 
429
                Namespace:   namespace,
 
430
                Home:        "$HOME",
433
431
                Target:      actualBinPath,
434
 
                Path:        pkgPath,
435
432
                AaProfile:   aaProfile,
436
 
                UdevAppName: udevPartName,
437
 
                Namespace:   namespace,
438
 
        }
 
433
        }
 
434
 
 
435
        oldVars := []string{}
 
436
        for _, envVar := range append(
 
437
                helpers.GetDeprecatedBasicSnapEnvVars(wrapperData),
 
438
                helpers.GetDeprecatedUserSnapEnvVars(wrapperData)...) {
 
439
                oldVars = append(oldVars, quoteEnvVar(envVar))
 
440
        }
 
441
        wrapperData.OldAppVars = strings.Join(oldVars, "\n")
 
442
 
 
443
        newVars := []string{}
 
444
        for _, envVar := range append(
 
445
                helpers.GetBasicSnapEnvVars(wrapperData),
 
446
                helpers.GetUserSnapEnvVars(wrapperData)...) {
 
447
                newVars = append(newVars, quoteEnvVar(envVar))
 
448
        }
 
449
        wrapperData.NewAppVars = strings.Join(newVars, "\n")
 
450
 
439
451
        t.Execute(&templateOut, wrapperData)
440
452
 
441
453
        return templateOut.String(), nil