2022-02-09 19:42:28 +03:00
/ *
_____ _____ _____ ____ ______ _____ -- -- --
| | | | | | | | | | | | |
| | | | | | | | | | | | |
| -- - | | | | | -- -- - | | -- -- | | | -- -- - | | -- -- - -- -- --
| | | | | | | | | | | | |
| ____ | | _____ | ____ | | ____ | | | _____ | _____ | | _____ | _____
Licensed under the MIT License < http : //opensource.org/licenses/MIT>.
2024-01-02 13:10:39 +03:00
Copyright © 2020 - 2024 Microsoft Corporation . All rights reserved .
2022-02-09 19:42:28 +03:00
Author : < blobfusedev @ microsoft . com >
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 cmd
import (
2022-03-15 07:29:21 +03:00
"encoding/xml"
2022-09-08 23:09:32 +03:00
"errors"
2022-02-09 19:42:28 +03:00
"fmt"
2023-05-10 12:23:05 +03:00
"io"
2022-02-09 19:42:28 +03:00
"net/http"
"os"
"strings"
"time"
2022-07-27 09:49:36 +03:00
"github.com/Azure/azure-storage-fuse/v2/common"
"github.com/Azure/azure-storage-fuse/v2/common/log"
2022-02-09 19:42:28 +03:00
"github.com/spf13/cobra"
)
2022-03-15 07:29:21 +03:00
type VersionFilesList struct {
XMLName xml . Name ` xml:"EnumerationResults" `
Blobs [ ] Blob ` xml:"Blobs>Blob" `
}
type Blob struct {
XMLName xml . Name ` xml:"Blob" `
Name string ` xml:"Name" `
2022-02-09 19:42:28 +03:00
}
var disableVersionCheck bool
var rootCmd = & cobra . Command {
2022-04-16 02:35:57 +03:00
Use : "blobfuse2" ,
Short : "Blobfuse2 is an open source project developed to provide a virtual filesystem backed by the Azure Storage." ,
Long : "Blobfuse2 is an open source project developed to provide a virtual filesystem backed by the Azure Storage. It uses the fuse protocol to communicate with the Linux FUSE kernel module, and implements the filesystem operations using the Azure Storage REST APIs." ,
Version : common . Blobfuse2Version ,
FlagErrorHandling : cobra . ExitOnError ,
2022-09-08 23:09:32 +03:00
SilenceUsage : true ,
RunE : func ( cmd * cobra . Command , args [ ] string ) error {
2022-02-09 19:42:28 +03:00
if ! disableVersionCheck {
2022-09-08 23:09:32 +03:00
err := VersionCheck ( )
if err != nil {
return err
}
2022-02-09 19:42:28 +03:00
}
2022-09-08 23:09:32 +03:00
return errors . New ( "missing command options\n\nDid you mean this?\n\tblobfuse2 mount\n\nRun 'blobfuse2 --help' for usage" )
2022-02-09 19:42:28 +03:00
} ,
}
2022-03-15 07:29:21 +03:00
// check if the version file exists in the container
func checkVersionExists ( versionUrl string ) bool {
resp , err := http . Get ( versionUrl )
if err != nil {
2022-09-08 23:09:32 +03:00
log . Err ( "checkVersionExists: error getting version file from container [%s]" , err . Error ( ) )
2022-03-15 07:29:21 +03:00
return false
}
return resp . StatusCode != 404
}
2022-10-05 19:03:52 +03:00
// getRemoteVersion : From public container get the latest blobfuse version
2022-03-15 07:29:21 +03:00
func getRemoteVersion ( req string ) ( string , error ) {
resp , err := http . Get ( req )
if err != nil {
2022-09-08 23:09:32 +03:00
log . Err ( "getRemoteVersion: error listing version file from container [%s]" , err . Error ( ) )
2022-03-15 07:29:21 +03:00
return "" , err
}
2023-05-10 12:23:05 +03:00
body , err := io . ReadAll ( resp . Body )
2022-03-15 07:29:21 +03:00
if err != nil {
2022-09-08 23:09:32 +03:00
log . Err ( "getRemoteVersion: error reading body of response [%s]" , err . Error ( ) )
2022-03-15 07:29:21 +03:00
return "" , err
}
var versionList VersionFilesList
err = xml . Unmarshal ( body , & versionList )
if err != nil {
2022-09-08 23:09:32 +03:00
log . Err ( "getRemoteVersion: error unmarshalling xml response [%s]" , err . Error ( ) )
2022-03-15 07:29:21 +03:00
return "" , err
}
if len ( versionList . Blobs ) != 1 {
return "" , fmt . Errorf ( "unable to get latest version" )
}
versionName := strings . Split ( versionList . Blobs [ 0 ] . Name , "/" ) [ 1 ]
return versionName , nil
}
2022-10-05 19:03:52 +03:00
// beginDetectNewVersion : Get latest release version and compare if user needs an upgrade or not
2022-02-09 19:42:28 +03:00
func beginDetectNewVersion ( ) chan interface { } {
completed := make ( chan interface { } )
stderr := os . Stderr
go func ( ) {
defer close ( completed )
2022-03-15 07:29:21 +03:00
latestVersionUrl := common . Blobfuse2ListContainerURL + "?restype=container&comp=list&prefix=latest/"
remoteVersion , err := getRemoteVersion ( latestVersionUrl )
2022-02-09 19:42:28 +03:00
if err != nil {
2022-09-08 23:09:32 +03:00
log . Err ( "beginDetectNewVersion: error getting latest version [%s]" , err . Error ( ) )
2022-12-23 15:07:40 +03:00
if strings . Contains ( err . Error ( ) , "no such host" ) {
log . Err ( "beginDetectNewVersion: check your network connection and proxy settings" )
}
2022-09-08 23:09:32 +03:00
completed <- err . Error ( )
2022-02-09 19:42:28 +03:00
return
}
local , err := common . ParseVersion ( common . Blobfuse2Version )
if err != nil {
2022-09-08 23:09:32 +03:00
log . Err ( "beginDetectNewVersion: error parsing Blobfuse2Version [%s]" , err . Error ( ) )
completed <- err . Error ( )
2022-02-09 19:42:28 +03:00
return
}
remote , err := common . ParseVersion ( remoteVersion )
if err != nil {
2022-09-08 23:09:32 +03:00
log . Err ( "beginDetectNewVersion: error parsing remoteVersion [%s]" , err . Error ( ) )
completed <- err . Error ( )
2022-02-09 19:42:28 +03:00
return
}
2024-07-29 12:43:48 +03:00
warningsUrl := common . Blobfuse2ListContainerURL + "/securitywarnings/" + common . Blobfuse2Version
hasWarnings := checkVersionExists ( warningsUrl )
if hasWarnings {
// This version has known issues associated with it
// Check whether the version has been blocked by the dev team or not.
blockedVersions := common . Blobfuse2ListContainerURL + "/blockedversions/" + common . Blobfuse2Version
isBlocked := checkVersionExists ( blockedVersions )
if isBlocked {
// This version is blocked and customer shall not be allowed to use this.
blockedPage := common . BlobFuse2BlockingURL + "#" + strings . ReplaceAll ( strings . ReplaceAll ( common . Blobfuse2Version , "." , "" ) , "~" , "" )
fmt . Fprintf ( stderr , "PANIC: Visit %s to see the list of known issues blocking your current version [%s]\n" , blockedPage , common . Blobfuse2Version )
log . Warn ( "PANIC: Visit %s to see the list of known issues blocking your current version [%s]\n" , blockedPage , common . Blobfuse2Version )
os . Exit ( 1 )
} else {
// This version is not blocked but has know issues list which customer shall visit.
warningsPage := common . BlobFuse2WarningsURL + "#" + strings . ReplaceAll ( strings . ReplaceAll ( common . Blobfuse2Version , "." , "" ) , "~" , "" )
fmt . Fprintf ( stderr , "WARNING: Visit %s to see the list of known issues associated with your current version [%s]\n" , warningsPage , common . Blobfuse2Version )
log . Warn ( "WARNING: Visit %s to see the list of known issues associated with your current version [%s]\n" , warningsPage , common . Blobfuse2Version )
}
}
2022-02-09 19:42:28 +03:00
if local . OlderThan ( * remote ) {
executablePathSegments := strings . Split ( strings . Replace ( os . Args [ 0 ] , "\\" , "/" , - 1 ) , "/" )
executableName := executablePathSegments [ len ( executablePathSegments ) - 1 ]
2022-02-16 09:44:03 +03:00
log . Info ( "beginDetectNewVersion: A new version of Blobfuse2 is available. Current Version=%s, Latest Version=%s" , common . Blobfuse2Version , remoteVersion )
2022-09-08 23:09:32 +03:00
fmt . Fprintf ( stderr , "*** " + executableName + ": A new version [%s] is available. Consider upgrading to latest version for bug-fixes & new features. ***\n" , remoteVersion )
log . Info ( "*** " + executableName + ": A new version [%s] is available. Consider upgrading to latest version for bug-fixes & new features. ***\n" , remoteVersion )
2022-02-16 09:44:03 +03:00
2022-09-08 23:09:32 +03:00
completed <- "A new version of Blobfuse2 is available"
2022-02-09 19:42:28 +03:00
}
} ( )
return completed
}
2022-10-05 19:03:52 +03:00
// VersionCheck : Start version check and wait for 8 seconds to complete otherwise just timeout and move on
2022-02-16 09:44:03 +03:00
func VersionCheck ( ) error {
select {
//either wait till this routine completes or timeout if it exceeds 8 secs
case <- beginDetectNewVersion ( ) :
case <- time . After ( 8 * time . Second ) :
return fmt . Errorf ( "unable to obtain latest version information. please check your internet connection" )
}
return nil
}
2022-10-05 19:03:52 +03:00
// ignoreCommand : There are command implicitly added by cobra itself, while parsing we need to ignore these commands
func ignoreCommand ( cmdArgs [ ] string ) bool {
ignoreCmds := [ ] string { "completion" , "help" }
if len ( cmdArgs ) > 0 {
for _ , c := range ignoreCmds {
if c == cmdArgs [ 0 ] {
return true
}
}
}
return false
}
// parseArgs : Depending upon inputs are coming from /etc/fstab or CLI, parameter style may vary.
2023-05-10 12:23:05 +03:00
// -- /etc/fstab example : blobfuse2 mount <dir> -o suid,nodev,--config-file=config.yaml,--use-adls=true,allow_other
// -- cli command : blobfuse2 mount <dir> -o suid,nodev --config-file=config.yaml --use-adls=true -o allow_other
// -- As we need to support both the ways, here we convert the /etc/fstab style (comma separated list) to standard cli ways
2022-10-05 19:03:52 +03:00
func parseArgs ( cmdArgs [ ] string ) [ ] string {
// Ignore binary name, rest all are arguments to blobfuse2
cmdArgs = cmdArgs [ 1 : ]
cmd , _ , err := rootCmd . Find ( cmdArgs )
if err != nil && cmd == rootCmd && ! ignoreCommand ( cmdArgs ) {
/ * / etc / fstab has a standard format and it goes like "<binary> <mount point> <type> <options>"
* as here we can not give any subcommand like "blobfuse2 mount" ( giving this will assume mount is mount point )
* we need to assume ' mount ' being default sub command .
* To do so , we just ignore the implicit commands handled by cobra and then try to check if input matches any of
* our subcommands or not . If not , we assume user has executed mount command without specifying mount subcommand
* so inject mount command in the cli options so that rest of the handling just assumes user gave mount subcommand .
* /
cmdArgs = append ( [ ] string { "mount" } , cmdArgs ... )
}
// Check for /etc/fstab style inputs
args := make ( [ ] string , 0 )
for i := 0 ; i < len ( cmdArgs ) ; i ++ {
// /etc/fstab will give everything in comma separated list with -o option
if cmdArgs [ i ] == "-o" {
i ++
if i < len ( cmdArgs ) {
bfuseArgs := make ( [ ] string , 0 )
lfuseArgs := make ( [ ] string , 0 )
// Check if ',' exists in arguments or not. If so we assume it might be coming from /etc/fstab
opts := strings . Split ( cmdArgs [ i ] , "," )
for _ , o := range opts {
// If we got comma separated list then all blobfuse specific options needs to be extracted out
// as those shall not be part of -o list which for us means libfuse options
if strings . HasPrefix ( o , "--" ) {
bfuseArgs = append ( bfuseArgs , o )
} else {
lfuseArgs = append ( lfuseArgs , o )
}
}
// Extract and add libfuse options with -o
if len ( lfuseArgs ) > 0 {
args = append ( args , "-o" , strings . Join ( lfuseArgs , "," ) )
}
// Extract and add blobfuse specific options sepratly
if len ( bfuseArgs ) > 0 {
args = append ( args , bfuseArgs ... )
}
}
} else {
// If any option is without -o then keep it as is (assuming its directly from cli)
args = append ( args , cmdArgs [ i ] )
}
}
return args
}
// Execute : Actual command execution starts from here
2022-02-09 19:42:28 +03:00
func Execute ( ) error {
2022-10-05 19:03:52 +03:00
parsedArgs := parseArgs ( os . Args )
rootCmd . SetArgs ( parsedArgs )
2022-09-08 23:09:32 +03:00
err := rootCmd . Execute ( )
if err != nil {
os . Exit ( 1 )
}
return err
2022-02-09 19:42:28 +03:00
}
func init ( ) {
rootCmd . PersistentFlags ( ) . BoolVar ( & disableVersionCheck , "disable-version-check" , false , "To disable version check that is performed automatically" )
}