mirror of https://github.com/portainer/portainer
fix(api): creates the data directory if not exist (#452)
parent
816c1ea448
commit
9ee652c818
|
@ -29,6 +29,11 @@ func main() {
|
||||||
Logo: *flags.Logo,
|
Logo: *flags.Logo,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fileService, err := file.NewService(*flags.Data, "")
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
var store = bolt.NewStore(*flags.Data)
|
var store = bolt.NewStore(*flags.Data)
|
||||||
err = store.Open()
|
err = store.Open()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -41,11 +46,6 @@ func main() {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
fileService, err := file.NewService(*flags.Data)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
var cryptoService portainer.CryptoService = &crypto.Service{}
|
var cryptoService portainer.CryptoService = &crypto.Service{}
|
||||||
|
|
||||||
// Initialize the active endpoint from the CLI only if there is no
|
// Initialize the active endpoint from the CLI only if there is no
|
||||||
|
|
|
@ -1,13 +1,12 @@
|
||||||
package file
|
package file
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"strconv"
|
|
||||||
|
|
||||||
"github.com/portainer/portainer"
|
"github.com/portainer/portainer"
|
||||||
|
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
|
"strconv"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
@ -21,18 +20,26 @@ const (
|
||||||
TLSKeyFile = "key.pem"
|
TLSKeyFile = "key.pem"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Service represents a service for managing files.
|
// Service represents a service for managing files and directories.
|
||||||
type Service struct {
|
type Service struct {
|
||||||
|
dataStorePath string
|
||||||
fileStorePath string
|
fileStorePath string
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewService initializes a new service.
|
// NewService initializes a new service. It creates a data directory and a directory to store files
|
||||||
func NewService(fileStorePath string) (*Service, error) {
|
// inside this directory if they don't exist.
|
||||||
|
func NewService(dataStorePath, fileStorePath string) (*Service, error) {
|
||||||
service := &Service{
|
service := &Service{
|
||||||
fileStorePath: fileStorePath,
|
dataStorePath: dataStorePath,
|
||||||
|
fileStorePath: path.Join(dataStorePath, fileStorePath),
|
||||||
}
|
}
|
||||||
|
|
||||||
err := service.createFolderInStoreIfNotExist(TLSStorePath)
|
err := createDirectoryIfNotExist(dataStorePath, 0755)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
err = service.createDirectoryInStoreIfNotExist(TLSStorePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -44,7 +51,7 @@ func NewService(fileStorePath string) (*Service, error) {
|
||||||
func (service *Service) StoreTLSFile(endpointID portainer.EndpointID, fileType portainer.TLSFileType, r io.Reader) error {
|
func (service *Service) StoreTLSFile(endpointID portainer.EndpointID, fileType portainer.TLSFileType, r io.Reader) error {
|
||||||
ID := strconv.Itoa(int(endpointID))
|
ID := strconv.Itoa(int(endpointID))
|
||||||
endpointStorePath := path.Join(TLSStorePath, ID)
|
endpointStorePath := path.Join(TLSStorePath, ID)
|
||||||
err := service.createFolderInStoreIfNotExist(endpointStorePath)
|
err := service.createDirectoryInStoreIfNotExist(endpointStorePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -97,12 +104,20 @@ func (service *Service) DeleteTLSFiles(endpointID portainer.EndpointID) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// createFolderInStoreIfNotExist creates a new folder in the file store if it doesn't exists on the file system.
|
// createDirectoryInStoreIfNotExist creates a new directory in the file store if it doesn't exists on the file system.
|
||||||
func (service *Service) createFolderInStoreIfNotExist(name string) error {
|
func (service *Service) createDirectoryInStoreIfNotExist(name string) error {
|
||||||
path := path.Join(service.fileStorePath, name)
|
path := path.Join(service.fileStorePath, name)
|
||||||
|
return createDirectoryIfNotExist(path, 0700)
|
||||||
|
}
|
||||||
|
|
||||||
|
// createDirectoryIfNotExist creates a directory if it doesn't exists on the file system.
|
||||||
|
func createDirectoryIfNotExist(path string, mode uint32) error {
|
||||||
_, err := os.Stat(path)
|
_, err := os.Stat(path)
|
||||||
if os.IsNotExist(err) {
|
if os.IsNotExist(err) {
|
||||||
os.Mkdir(path, 0600)
|
err = os.Mkdir(path, os.FileMode(mode))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
} else if err != nil {
|
} else if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue