/
remote_status.go
164 lines (140 loc) · 4.1 KB
/
remote_status.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
// Copyright (c) Contributors to the Apptainer project, established as
// Apptainer a Series of LF Projects LLC.
// For website terms of use, trademark policy, privacy policy and other
// project policies see https://lfprojects.org/policies
// Copyright (c) 2020, Control Command Inc. All rights reserved.
// Copyright (c) 2019-2023, Sylabs Inc. All rights reserved.
// This software is licensed under a 3-clause BSD license. Please consult the
// LICENSE.md file distributed with the sources of this project regarding your
// rights to use or distribute this software.
package apptainer
import (
"fmt"
"os"
"sort"
"text/tabwriter"
"github.com/apptainer/apptainer/internal/pkg/client/library"
"github.com/apptainer/apptainer/internal/pkg/remote"
"github.com/apptainer/apptainer/internal/pkg/remote/endpoint"
"github.com/apptainer/apptainer/pkg/sylog"
containerclient "github.com/apptainer/container-library-client/client"
"golang.org/x/text/cases"
"golang.org/x/text/language"
)
const statusLine = "%s\t%s\t%s\t%s\n"
type status struct {
name string
uri string
status string
version string
}
// RemoteStatus checks status of services related to an endpoint
// If the supplied remote name is an empty string, it will attempt
// to use the default remote.
func RemoteStatus(usrConfigFile, name string) (err error) {
if name != "" {
sylog.Infof("Checking status of remote: %s", name)
} else {
sylog.Infof("Checking status of default remote.")
}
// opening config file
file, err := os.OpenFile(usrConfigFile, os.O_RDONLY|os.O_CREATE, 0o600)
if err != nil {
if os.IsNotExist(err) {
return fmt.Errorf("no remote configurations")
}
return fmt.Errorf("while opening remote config file: %s", err)
}
defer file.Close()
// read file contents to config struct
c, err := remote.ReadFrom(file)
if err != nil {
return fmt.Errorf("while parsing remote config data: %s", err)
}
if err := syncSysConfig(c); err != nil {
return err
}
var e *endpoint.Config
if name == "" {
e, err = c.GetDefault()
} else {
e, err = c.GetRemote(name)
}
if err != nil {
return err
}
sps, err := e.GetAllServices()
if err != nil {
return fmt.Errorf("while retrieving services: %s", err)
}
ch := make(chan *status)
for name, sp := range sps {
name := name
for _, service := range sp {
service := service
go func() {
ch <- doStatusCheck(name, service)
}()
}
}
// map storing statuses by name
smap := make(map[string]*status)
for _, sp := range sps {
for range sp {
s := <-ch
if s == nil {
continue
}
smap[s.name] = s
}
}
// list in alphanumeric order
names := make([]string, 0, len(smap))
for n := range smap {
names = append(names, n)
}
sort.Strings(names)
tw := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0)
fmt.Fprintf(tw, statusLine, "SERVICE", "STATUS", "VERSION", "URI")
for _, n := range names {
s := smap[n]
fmt.Fprintf(tw, statusLine, cases.Title(language.English).String(s.name), s.status, s.version, s.uri)
}
tw.Flush()
libClientConfigURI := ""
libClientConfig, err := e.LibraryClientConfig(libClientConfigURI)
if err != nil {
return fmt.Errorf("could not get library client configuration: %v", err)
}
printLoggedInIdentity(libClientConfig)
return doTokenCheck(e)
}
func doStatusCheck(name string, sp endpoint.Service) *status {
uri := sp.URI()
version, err := sp.Status()
if err != nil {
if err == endpoint.ErrStatusNotSupported {
return nil
}
return &status{name: name, uri: uri, status: "N/A"}
}
return &status{name: name, uri: uri, status: "OK", version: version}
}
func printLoggedInIdentity(config *containerclient.Config) {
username, email, err := library.GetIdentity(config)
if err == nil && len(username) > 0 {
fmt.Printf("\nLogged in as: %s <%s>\n\n", username, email)
}
}
func doTokenCheck(e *endpoint.Config) error {
if e.Token == "" {
fmt.Println("\nNo authentication token set (logged out).")
return nil
}
if err := e.VerifyToken(""); err != nil {
fmt.Println("\nAuthentication token is invalid (please login again).")
return err
}
fmt.Println("\nValid authentication token set (logged in).")
return nil
}