summaryrefslogtreecommitdiff
path: root/dryad/rusalka.go
blob: 7549308da50030e006c3b9af347ef655bbd60e0a (plain)
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
/*
 *  Copyright (c) 2017-2018 Samsung Electronics Co., Ltd All Rights Reserved
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License
 */

// Package dryad provides:
// * implementation of Dryad interface
// * utilities to manage Dryad and its users
package dryad

import (
	"context"
	"crypto/rsa"
	"fmt"

	. "git.tizen.org/tools/boruta"
	"git.tizen.org/tools/muxpi/sw/nanopi/stm"
	"golang.org/x/crypto/ssh"
)

// Rusalka implements Dryad interface. It is intended to be used on NanoPi connected to MuxPi.
// It is not safe for concurrent use.
type Rusalka struct {
	Dryad
	dryadUser         *borutaUser
	stm               *stmHelper
	cancelMaintenance context.CancelFunc
}

// NewRusalka returns Dryad interface to Rusalka.
func NewRusalka(stmConn stm.Interface, username string, groups []string) Dryad {
	return &Rusalka{
		dryadUser: newBorutaUser(username, groups),
		stm:       &stmHelper{stmConn},
	}
}

// PutInMaintenance is part of implementation of Dryad interface.
// Connection to STM is being opened only for the maintenance actions.
// Otherwise it may make it unusable for other STM users. It is closed
// when blinkMaintenanceLED exits.
func (r *Rusalka) PutInMaintenance(msg string) error {
	err := r.stm.printMessage(msg)
	if err != nil {
		return err
	}
	var ctx context.Context
	ctx, r.cancelMaintenance = context.WithCancel(context.Background())
	go r.stm.blinkMaintenanceLED(ctx)
	return nil
}

// Prepare is part of implementation of Dryad interface. Call to Prepare stops LED blinking.
func (r *Rusalka) Prepare(key *rsa.PublicKey) (err error) {
	// Stop maintenance.
	if r.cancelMaintenance != nil {
		r.cancelMaintenance()
		r.cancelMaintenance = nil
	}
	// Remove/Add user.
	err = r.dryadUser.delete()
	if err != nil {
		return fmt.Errorf("user removal failed: %s", err)
	}
	err = r.dryadUser.add()
	if err != nil {
		return fmt.Errorf("user creation failed: %s", err)
	}
	// Verify user's existance.
	err = r.dryadUser.update()
	if err != nil {
		return fmt.Errorf("user information update failed: %s", err)
	}
	// Prepare SSH access (it can't fail as key is of type rsa.PublicKey).
	sshPubKey, _ := ssh.NewPublicKey(key)
	// TODO: use ssh.PublicKey instead.
	return r.dryadUser.generateAndInstallKey(sshPubKey)
}

// Healthcheck is part of implementation of Dryad interface.
func (r *Rusalka) Healthcheck() (err error) {
	return r.stm.powerTick()
}