/
files.go
150 lines (128 loc) · 5.2 KB
/
files.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
/* (c) 2019, Blender Foundation
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package azstorage
import (
"context"
"fmt"
"net/url"
"strings"
"github.com/Azure/flamenco-manager-azure/flamenco"
"github.com/Azure/azure-storage-file-go/azfile"
"github.com/sirupsen/logrus"
"github.com/Azure/flamenco-manager-azure/azconfig"
)
const (
defaultQuotaInGB int32 = 5 * 1024 // SMB share quota, in gigabytes
statusShareAlreadyExists = "ShareAlreadyExists"
)
// Share has some options for SMB share mountpoints.
type Share struct {
fileMode int16
}
var (
// DefaultSMBShares has the shares that will be mounted on both the Manager and the Workers.
DefaultSMBShares = map[string]Share{
"flamenco-resources": Share{fileMode: 0775},
"flamenco-input": Share{fileMode: 0660},
"flamenco-output": Share{fileMode: 0660},
}
)
// EnsureFileShares sets up the SMB shares. Returns fstab lines to mount them.
func EnsureFileShares(ctx context.Context, config azconfig.AZConfig) string {
fstab := []string{}
shareURL := getShareURL(config)
for shareName := range DefaultSMBShares {
createFileShare(ctx, shareURL, shareName)
fstabLine := GetFSTabLine(config, shareName)
fstab = append(fstab, fstabLine)
}
return strings.Join(fstab, "\n") + "\n"
}
// GetFSTabLine returns the /etc/fstab line for the given share.
func GetFSTabLine(config azconfig.AZConfig, shareName string) string {
mountOpts := GetMountOptions(config, shareName)
return fmt.Sprintf(
"//%s.file.core.windows.net/%s /mnt/%s cifs %s 0 0",
config.StorageAccountName,
shareName, shareName,
mountOpts,
)
}
// GetMountOptions returns the mount options for the given SMB share.
func GetMountOptions(config azconfig.AZConfig, shareName string) string {
shareOptions, found := DefaultSMBShares[shareName]
if !found {
logrus.WithField("shareName", shareName).Fatal("share name unknown")
}
return fmt.Sprintf(
"vers=3.0,username=%s,password=%s,dir_mode=0770,file_mode=%#o,gid=%s,forcegid,sec=ntlmssp,mfsymlinks",
config.StorageCreds.Username, config.StorageCreds.Password,
shareOptions.fileMode, flamenco.UnixGroupName,
)
}
func getShareURL(config azconfig.AZConfig) azfile.ServiceURL {
logger := logrus.WithFields(logrus.Fields{
"storageAccount": config.StorageAccountName,
})
// Use your Storage account's name and key to create a credential object; this is used to access your account.
credential, err := azfile.NewSharedKeyCredential(config.StorageCreds.Username, config.StorageCreds.Password)
if err != nil {
logger.WithError(err).Fatal("unable to construct credentials for Azure Files")
}
// Create a request pipeline that is used to process HTTP(S) requests and responses. It requires
// your account credentials. In more advanced scenarios, you can configure telemetry, retry policies,
// logging, and other options. Also, you can configure multiple request pipelines for different scenarios.
pipeline := azfile.NewPipeline(credential, azfile.PipelineOptions{})
// From the Azure portal, get your Storage account file service URL endpoint.
// The URL typically looks like this:
topURL, err := url.Parse(fmt.Sprintf("https://%s.file.core.windows.net", config.StorageAccountName))
if err != nil {
logger.WithError(err).Fatal("unable to construct URL")
}
// Create an ServiceURL object that wraps the service URL and a request pipeline.
return azfile.NewServiceURL(*topURL, pipeline)
}
// createFileShare creates an SMB file share.
func createFileShare(ctx context.Context, serviceURL azfile.ServiceURL, shareName string) {
shareName = strings.ToLower(shareName)
logger := logrus.WithFields(logrus.Fields{
"shareName": shareName,
})
logger.Info("ensuring SMB share exists")
shareURL := serviceURL.NewShareURL(shareName)
_, err := shareURL.Create(ctx, azfile.Metadata{}, defaultQuotaInGB)
if err != nil {
storageErr, ok := err.(azfile.StorageError)
if !ok {
logger.WithError(err).Fatal("unable to create SMB share")
}
if storageErr.ServiceCode() == statusShareAlreadyExists {
logger.Debug("SMB share already exists")
return
}
logger.WithFields(logrus.Fields{
"serviceCode": storageErr.ServiceCode(),
logrus.ErrorKey: storageErr,
}).Fatal("unable to create SMB share")
}
logger.Info("SMB share created")
}