Remove 99% of deprecated ioutil usage

Signed-off-by: David Gageot <david.gageot@docker.com>
This commit is contained in:
David Gageot 2022-10-07 20:28:10 +02:00
parent 8ef4fa3483
commit d4e132021a
No known key found for this signature in database
GPG Key ID: 93C3F22BE5D3A40B
21 changed files with 78 additions and 89 deletions

View File

@ -4,7 +4,6 @@ import (
"bytes" "bytes"
"flag" "flag"
"fmt" "fmt"
"io/ioutil"
"log" "log"
"os" "os"
"path/filepath" "path/filepath"
@ -74,13 +73,13 @@ func main() {
} }
defer syscall.Unmount(mount, 0) defer syscall.Unmount(mount, 0)
files, err := ioutil.ReadDir(dir) files, err := os.ReadDir(dir)
if err != nil { if err != nil {
log.Fatalf("Cannot read directory %s: %s", dir, err) log.Fatalf("Cannot read directory %s: %s", dir, err)
} }
for _, file := range files { for _, file := range files {
contents, err := ioutil.ReadFile(filepath.Join(dir, file.Name())) contents, err := os.ReadFile(filepath.Join(dir, file.Name()))
if err != nil { if err != nil {
log.Fatalf("Cannot read file %s: %s", file.Name(), err) log.Fatalf("Cannot read file %s: %s", file.Name(), err)
} }

View File

@ -3,7 +3,6 @@ package main
import ( import (
"bufio" "bufio"
"encoding/csv" "encoding/csv"
"io/ioutil"
"log" "log"
"os" "os"
"os/exec" "os/exec"
@ -109,7 +108,7 @@ func cgroupList() []string {
// write a file, eg sysfs // write a file, eg sysfs
func write(path string, value string) { func write(path string, value string) {
err := ioutil.WriteFile(path, []byte(value), 0600) err := os.WriteFile(path, []byte(value), 0600)
if err != nil { if err != nil {
log.Printf("cannot write to %s: %v", path, err) log.Printf("cannot write to %s: %v", path, err)
} }
@ -117,7 +116,7 @@ func write(path string, value string) {
// read a file, eg sysfs, strip whitespace, empty string if does not exist // read a file, eg sysfs, strip whitespace, empty string if does not exist
func read(path string) string { func read(path string) string {
data, err := ioutil.ReadFile(path) data, err := os.ReadFile(path)
if err != nil { if err != nil {
return "" return ""
} }
@ -127,7 +126,7 @@ func read(path string) string {
// read a directory // read a directory
func readdir(path string) []string { func readdir(path string) []string {
names := []string{} names := []string{}
files, err := ioutil.ReadDir(path) files, err := os.ReadDir(path)
if err != nil { if err != nil {
log.Printf("cannot read directory %s: %v", path, err) log.Printf("cannot read directory %s: %v", path, err)
return names return names

View File

@ -3,7 +3,6 @@ package main
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"strings" "strings"
@ -47,7 +46,7 @@ type Interface struct {
func getRuntimeConfig(path string) Runtime { func getRuntimeConfig(path string) Runtime {
var runtime Runtime var runtime Runtime
conf, err := ioutil.ReadFile(filepath.Join(path, "runtime.json")) conf, err := os.ReadFile(filepath.Join(path, "runtime.json"))
if err != nil { if err != nil {
// if it does not exist it is fine to return an empty runtime, to not do anything // if it does not exist it is fine to return an empty runtime, to not do anything
if os.IsNotExist(err) { if os.IsNotExist(err) {
@ -134,7 +133,7 @@ func newCgroup(cgroup string) error {
return nil return nil
} }
// a cgroupv1 cgroup is a directory under all directories in /sys/fs/cgroup // a cgroupv1 cgroup is a directory under all directories in /sys/fs/cgroup
dirs, err := ioutil.ReadDir("/sys/fs/cgroup") dirs, err := os.ReadDir("/sys/fs/cgroup")
if err != nil { if err != nil {
return err return err
} }

View File

@ -2,7 +2,6 @@ package main
import ( import (
"io" "io"
"io/ioutil"
"os" "os"
"os/exec" "os/exec"
"path" "path"
@ -38,12 +37,12 @@ func runcInit(rootPath, serviceType string) int {
} }
// get files; note ReadDir already sorts them // get files; note ReadDir already sorts them
files, err := ioutil.ReadDir(rootPath) files, err := os.ReadDir(rootPath)
if err != nil { if err != nil {
log.Fatalf("Cannot read files in %s: %v", rootPath, err) log.Fatalf("Cannot read files in %s: %v", rootPath, err)
} }
tmpdir, err := ioutil.TempDir("", filepath.Base(rootPath)) tmpdir, err := os.MkdirTemp("", filepath.Base(rootPath))
if err != nil { if err != nil {
log.Fatalf("Cannot create temporary directory: %v", err) log.Fatalf("Cannot create temporary directory: %v", err)
} }
@ -105,7 +104,7 @@ func runcInit(rootPath, serviceType string) int {
// skip cleanup on error for debug // skip cleanup on error for debug
continue continue
} }
pf, err := ioutil.ReadFile(pidfile) pf, err := os.ReadFile(pidfile)
if err != nil { if err != nil {
log.Printf("Cannot read pidfile: %v", err) log.Printf("Cannot read pidfile: %v", err)
status = 1 status = 1

View File

@ -5,7 +5,6 @@ import (
"flag" "flag"
"fmt" "fmt"
"io" "io"
"io/ioutil"
"os" "os"
"os/exec" "os/exec"
"path/filepath" "path/filepath"
@ -91,7 +90,7 @@ func systemInitCmd(ctx context.Context, args []string) {
stderr io.Writer = os.Stderr stderr io.Writer = os.Stderr
stdout io.Writer = os.Stdout stdout io.Writer = os.Stdout
) )
if b, err := ioutil.ReadFile(containerdOptsFile); err == nil { if b, err := os.ReadFile(containerdOptsFile); err == nil {
config, err := toml.LoadBytes(b) config, err := toml.LoadBytes(b)
if err != nil { if err != nil {
log.Fatalf("error reading toml file %s: %v", containerdOptsFile, err) log.Fatalf("error reading toml file %s: %v", containerdOptsFile, err)
@ -173,7 +172,7 @@ func systemInitCmd(ctx context.Context, args []string) {
} }
// Start up containers // Start up containers
files, err := ioutil.ReadDir(*path) files, err := os.ReadDir(*path)
// just skip if there is an error, eg no such path // just skip if there is an error, eg no such path
if err != nil { if err != nil {
return return

View File

@ -3,7 +3,6 @@ package main
import ( import (
"encoding/json" "encoding/json"
"flag" "flag"
"io/ioutil"
"os" "os"
"path" "path"
"strconv" "strconv"
@ -160,7 +159,7 @@ func main() {
log.Printf("Error during metadata probe: %s", err) log.Printf("Error during metadata probe: %s", err)
} }
err = ioutil.WriteFile(path.Join(ConfigPath, "provider"), []byte(p.String()), 0644) err = os.WriteFile(path.Join(ConfigPath, "provider"), []byte(p.String()), 0644)
if err != nil { if err != nil {
log.Printf("Error writing metadata provider: %s", err) log.Printf("Error writing metadata provider: %s", err)
} }
@ -173,7 +172,7 @@ func main() {
// Handle setting the hostname as a special case. We want to // Handle setting the hostname as a special case. We want to
// do this early and don't really want another container for it. // do this early and don't really want another container for it.
hostname, err := ioutil.ReadFile(path.Join(ConfigPath, Hostname)) hostname, err := os.ReadFile(path.Join(ConfigPath, Hostname))
if err == nil { if err == nil {
err := syscall.Sethostname(hostname) err := syscall.Sethostname(hostname)
if err != nil { if err != nil {
@ -196,7 +195,7 @@ func main() {
// Will create foobar/foo with mode 0644 and content "hello" // Will create foobar/foo with mode 0644 and content "hello"
func processUserData(basePath string, data []byte) error { func processUserData(basePath string, data []byte) error {
// Always write the raw data to a file // Always write the raw data to a file
err := ioutil.WriteFile(path.Join(basePath, "userdata"), data, 0644) err := os.WriteFile(path.Join(basePath, "userdata"), data, 0644)
if err != nil { if err != nil {
log.Printf("Could not write userdata: %s", err) log.Printf("Could not write userdata: %s", err)
return err return err
@ -223,7 +222,7 @@ func writeConfigFiles(target string, current Entry) {
log.Printf("Failed to parse permission %+v: %s", current, err) log.Printf("Failed to parse permission %+v: %s", current, err)
return return
} }
if err := ioutil.WriteFile(target, []byte(*current.Content), filemode); err != nil { if err := os.WriteFile(target, []byte(*current.Content), filemode); err != nil {
log.Printf("Failed to write %s: %s", target, err) log.Printf("Failed to write %s: %s", target, err)
return return
} }

View File

@ -3,14 +3,13 @@ package main
import ( import (
"bytes" "bytes"
"encoding/json" "encoding/json"
"io/ioutil"
"os" "os"
"path" "path"
"testing" "testing"
) )
func TestSampleConfig(t *testing.T) { func TestSampleConfig(t *testing.T) {
basePath, err := ioutil.TempDir("", "metadata") basePath, err := os.MkdirTemp("", "metadata")
if err != nil { if err != nil {
t.Fatalf("can't make a temp rootdir %v", err) t.Fatalf("can't make a temp rootdir %v", err)
} }
@ -82,7 +81,7 @@ func TestSerialization(t *testing.T) {
} }
func TestWriteSingleFile(t *testing.T) { func TestWriteSingleFile(t *testing.T) {
basePath, err := ioutil.TempDir(os.TempDir(), "metadata") basePath, err := os.MkdirTemp(os.TempDir(), "metadata")
if err != nil { if err != nil {
t.Fatalf("can't make a temp rootdir %v", err) t.Fatalf("can't make a temp rootdir %v", err)
} }
@ -98,7 +97,7 @@ func TestWriteSingleFile(t *testing.T) {
} }
func TestWriteEmptyFile(t *testing.T) { func TestWriteEmptyFile(t *testing.T) {
basePath, err := ioutil.TempDir(os.TempDir(), "metadata") basePath, err := os.MkdirTemp(os.TempDir(), "metadata")
if err != nil { if err != nil {
t.Fatalf("can't make a temp rootdir %v", err) t.Fatalf("can't make a temp rootdir %v", err)
} }
@ -114,7 +113,7 @@ func TestWriteEmptyFile(t *testing.T) {
} }
func TestWriteEmptyDirectory(t *testing.T) { func TestWriteEmptyDirectory(t *testing.T) {
basePath, err := ioutil.TempDir(os.TempDir(), "metadata") basePath, err := os.MkdirTemp(os.TempDir(), "metadata")
if err != nil { if err != nil {
t.Fatalf("can't make a temp rootdir %v", err) t.Fatalf("can't make a temp rootdir %v", err)
} }
@ -132,7 +131,7 @@ func TestWriteEmptyDirectory(t *testing.T) {
} }
func TestSetPermission(t *testing.T) { func TestSetPermission(t *testing.T) {
basePath, err := ioutil.TempDir(os.TempDir(), "metadata") basePath, err := os.MkdirTemp(os.TempDir(), "metadata")
if err != nil { if err != nil {
t.Fatalf("can't make a temp rootdir %v", err) t.Fatalf("can't make a temp rootdir %v", err)
} }
@ -155,7 +154,7 @@ func TestSetPermission(t *testing.T) {
} }
func TestDeepTree(t *testing.T) { func TestDeepTree(t *testing.T) {
basePath, err := ioutil.TempDir("", "metadata") basePath, err := os.MkdirTemp("", "metadata")
if err != nil { if err != nil {
t.Fatalf("can't make a temp rootdir %v", err) t.Fatalf("can't make a temp rootdir %v", err)
} }
@ -207,7 +206,7 @@ func assertPermission(t *testing.T, path string, expected os.FileMode) {
} }
func assertContent(t *testing.T, path, expected string) { func assertContent(t *testing.T, path, expected string) {
file, err := ioutil.ReadFile(path) file, err := os.ReadFile(path)
if err != nil { if err != nil {
t.Fatalf("can't read %v: %v", path, err) t.Fatalf("can't read %v: %v", path, err)
} }

View File

@ -2,7 +2,7 @@ package main
import ( import (
"fmt" "fmt"
"io/ioutil" "io"
"log" "log"
"net/http" "net/http"
"os" "os"
@ -37,7 +37,7 @@ func (p *ProviderAWS) Extract() ([]byte, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
err = ioutil.WriteFile(path.Join(ConfigPath, Hostname), hostname, 0644) err = os.WriteFile(path.Join(ConfigPath, Hostname), hostname, 0644)
if err != nil { if err != nil {
return nil, fmt.Errorf("AWS: Failed to write hostname: %s", err) return nil, fmt.Errorf("AWS: Failed to write hostname: %s", err)
} }
@ -79,7 +79,7 @@ func (p *ProviderAWS) Extract() ([]byte, error) {
func awsMetaGet(lookupName string, fileName string, fileMode os.FileMode) { func awsMetaGet(lookupName string, fileName string, fileMode os.FileMode) {
if lookupValue, err := awsGet(metaDataURL + lookupName); err == nil { if lookupValue, err := awsGet(metaDataURL + lookupName); err == nil {
// we got a value from the metadata server, now save to filesystem // we got a value from the metadata server, now save to filesystem
err = ioutil.WriteFile(path.Join(ConfigPath, fileName), lookupValue, fileMode) err = os.WriteFile(path.Join(ConfigPath, fileName), lookupValue, fileMode)
if err != nil { if err != nil {
// we couldn't save the file for some reason // we couldn't save the file for some reason
log.Printf("AWS: Failed to write %s:%s %s", fileName, lookupValue, err) log.Printf("AWS: Failed to write %s:%s %s", fileName, lookupValue, err)
@ -108,7 +108,7 @@ func awsGet(url string) ([]byte, error) {
if resp.StatusCode != 200 { if resp.StatusCode != 200 {
return nil, fmt.Errorf("AWS: Status not ok: %d", resp.StatusCode) return nil, fmt.Errorf("AWS: Status not ok: %d", resp.StatusCode)
} }
body, err := ioutil.ReadAll(resp.Body) body, err := io.ReadAll(resp.Body)
if err != nil { if err != nil {
return nil, fmt.Errorf("AWS: Failed to read http response: %s", err) return nil, fmt.Errorf("AWS: Failed to read http response: %s", err)
} }
@ -126,7 +126,7 @@ func (p *ProviderAWS) handleSSH() error {
return fmt.Errorf("Failed to create %s: %s", SSH, err) return fmt.Errorf("Failed to create %s: %s", SSH, err)
} }
err = ioutil.WriteFile(path.Join(ConfigPath, SSH, "authorized_keys"), sshKeys, 0600) err = os.WriteFile(path.Join(ConfigPath, SSH, "authorized_keys"), sshKeys, 0600)
if err != nil { if err != nil {
return fmt.Errorf("Failed to write ssh keys: %s", err) return fmt.Errorf("Failed to write ssh keys: %s", err)
} }

View File

@ -2,7 +2,7 @@ package main
import ( import (
"fmt" "fmt"
"io/ioutil" "os"
"path" "path"
"path/filepath" "path/filepath"
"strings" "strings"
@ -100,14 +100,14 @@ func FindCIs() []string {
// NewCDROM returns a new ProviderCDROM // NewCDROM returns a new ProviderCDROM
func NewCDROM(device string) *ProviderCDROM { func NewCDROM(device string) *ProviderCDROM {
mountPoint, err := ioutil.TempDir("", "cd") mountPoint, err := os.MkdirTemp("", "cd")
p := ProviderCDROM{device, mountPoint, err, []byte{}, []byte{}} p := ProviderCDROM{device, mountPoint, err, []byte{}, []byte{}}
if err == nil { if err == nil {
if p.err = p.mount(); p.err == nil { if p.err = p.mount(); p.err == nil {
// read the userdata - we read the spec file and the fallback, but eventually // read the userdata - we read the spec file and the fallback, but eventually
// will remove the fallback // will remove the fallback
for _, f := range userdataFiles { for _, f := range userdataFiles {
userdata, err := ioutil.ReadFile(path.Join(p.mountPoint, f)) userdata, err := os.ReadFile(path.Join(p.mountPoint, f))
// did we find a file? // did we find a file?
if err == nil && userdata != nil { if err == nil && userdata != nil {
p.userdata = userdata p.userdata = userdata
@ -118,7 +118,7 @@ func NewCDROM(device string) *ProviderCDROM {
p.err = fmt.Errorf("no userdata file found at any of %v", userdataFiles) p.err = fmt.Errorf("no userdata file found at any of %v", userdataFiles)
} }
// read the metadata // read the metadata
metadata, err := ioutil.ReadFile(path.Join(p.mountPoint, metadataFile)) metadata, err := os.ReadFile(path.Join(p.mountPoint, metadataFile))
// did we find a file? // did we find a file?
if err == nil && metadata != nil { if err == nil && metadata != nil {
p.metadata = metadata p.metadata = metadata

View File

@ -2,7 +2,7 @@ package main
import ( import (
"fmt" "fmt"
"io/ioutil" "io"
"log" "log"
"net/http" "net/http"
"os" "os"
@ -41,7 +41,7 @@ func (p *ProviderDigitalOcean) Extract() ([]byte, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
err = ioutil.WriteFile(path.Join(ConfigPath, Hostname), hostname, 0644) err = os.WriteFile(path.Join(ConfigPath, Hostname), hostname, 0644)
if err != nil { if err != nil {
return nil, fmt.Errorf("DigitalOcean: Failed to write hostname: %s", err) return nil, fmt.Errorf("DigitalOcean: Failed to write hostname: %s", err)
} }
@ -77,7 +77,7 @@ func (p *ProviderDigitalOcean) Extract() ([]byte, error) {
func digitalOceanMetaGet(lookupName string, fileName string, fileMode os.FileMode) { func digitalOceanMetaGet(lookupName string, fileName string, fileMode os.FileMode) {
if lookupValue, err := digitalOceanGet(digitalOceanMetaDataURL + lookupName); err == nil { if lookupValue, err := digitalOceanGet(digitalOceanMetaDataURL + lookupName); err == nil {
// we got a value from the metadata server, now save to filesystem // we got a value from the metadata server, now save to filesystem
err = ioutil.WriteFile(path.Join(ConfigPath, fileName), lookupValue, fileMode) err = os.WriteFile(path.Join(ConfigPath, fileName), lookupValue, fileMode)
if err != nil { if err != nil {
// we couldn't save the file for some reason // we couldn't save the file for some reason
log.Printf("DigitalOcean: Failed to write %s:%s %s", fileName, lookupValue, err) log.Printf("DigitalOcean: Failed to write %s:%s %s", fileName, lookupValue, err)
@ -106,7 +106,7 @@ func digitalOceanGet(url string) ([]byte, error) {
if resp.StatusCode != 200 { if resp.StatusCode != 200 {
return nil, fmt.Errorf("DigitalOcean: Status not ok: %d", resp.StatusCode) return nil, fmt.Errorf("DigitalOcean: Status not ok: %d", resp.StatusCode)
} }
body, err := ioutil.ReadAll(resp.Body) body, err := io.ReadAll(resp.Body)
if err != nil { if err != nil {
return nil, fmt.Errorf("DigitalOcean: Failed to read http response: %s", err) return nil, fmt.Errorf("DigitalOcean: Failed to read http response: %s", err)
} }
@ -124,7 +124,7 @@ func (p *ProviderDigitalOcean) handleSSH() error {
return fmt.Errorf("Failed to create %s: %s", SSH, err) return fmt.Errorf("Failed to create %s: %s", SSH, err)
} }
err = ioutil.WriteFile(path.Join(ConfigPath, SSH, "authorized_keys"), sshKeys, 0600) err = os.WriteFile(path.Join(ConfigPath, SSH, "authorized_keys"), sshKeys, 0600)
if err != nil { if err != nil {
return fmt.Errorf("Failed to write ssh keys: %s", err) return fmt.Errorf("Failed to write ssh keys: %s", err)
} }

View File

@ -1,7 +1,6 @@
package main package main
import ( import (
"io/ioutil"
"os" "os"
) )
@ -17,5 +16,5 @@ func (p fileProvider) Probe() bool {
} }
func (p fileProvider) Extract() ([]byte, error) { func (p fileProvider) Extract() ([]byte, error) {
return ioutil.ReadFile(string(p)) return os.ReadFile(string(p))
} }

View File

@ -2,7 +2,7 @@ package main
import ( import (
"fmt" "fmt"
"io/ioutil" "io"
"log" "log"
"net/http" "net/http"
"os" "os"
@ -43,7 +43,7 @@ func (p *ProviderGCP) Extract() ([]byte, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
err = ioutil.WriteFile(path.Join(ConfigPath, Hostname), hostname, 0644) err = os.WriteFile(path.Join(ConfigPath, Hostname), hostname, 0644)
if err != nil { if err != nil {
return nil, fmt.Errorf("GCP: Failed to write hostname: %s", err) return nil, fmt.Errorf("GCP: Failed to write hostname: %s", err)
} }
@ -81,7 +81,7 @@ func gcpGet(url string) ([]byte, error) {
if resp.StatusCode != 200 { if resp.StatusCode != 200 {
return nil, fmt.Errorf("GCP: Status not ok: %d", resp.StatusCode) return nil, fmt.Errorf("GCP: Status not ok: %d", resp.StatusCode)
} }
body, err := ioutil.ReadAll(resp.Body) body, err := io.ReadAll(resp.Body)
if err != nil { if err != nil {
return nil, fmt.Errorf("GCP: Failed to read http response: %s", err) return nil, fmt.Errorf("GCP: Failed to read http response: %s", err)
} }
@ -116,7 +116,7 @@ func (p *ProviderGCP) handleSSH() error {
rootKeys = rootKeys + parts[1] + "\n" rootKeys = rootKeys + parts[1] + "\n"
} }
} }
err = ioutil.WriteFile(path.Join(ConfigPath, SSH, "authorized_keys"), []byte(rootKeys), 0600) err = os.WriteFile(path.Join(ConfigPath, SSH, "authorized_keys"), []byte(rootKeys), 0600)
if err != nil { if err != nil {
return fmt.Errorf("Failed to write ssh keys: %s", err) return fmt.Errorf("Failed to write ssh keys: %s", err)
} }

View File

@ -3,7 +3,7 @@ package main
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"io/ioutil" "io"
"log" "log"
"net/http" "net/http"
"os" "os"
@ -38,7 +38,7 @@ func (p *ProviderHetzner) Extract() ([]byte, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
err = ioutil.WriteFile(path.Join(ConfigPath, Hostname), hostname, 0644) err = os.WriteFile(path.Join(ConfigPath, Hostname), hostname, 0644)
if err != nil { if err != nil {
return nil, fmt.Errorf("Hetzner: Failed to write hostname: %s", err) return nil, fmt.Errorf("Hetzner: Failed to write hostname: %s", err)
} }
@ -74,7 +74,7 @@ func (p *ProviderHetzner) Extract() ([]byte, error) {
func hetznerMetaGet(lookupName string, fileName string, fileMode os.FileMode) { func hetznerMetaGet(lookupName string, fileName string, fileMode os.FileMode) {
if lookupValue, err := hetznerGet(metaDataURL + lookupName); err == nil { if lookupValue, err := hetznerGet(metaDataURL + lookupName); err == nil {
// we got a value from the metadata server, now save to filesystem // we got a value from the metadata server, now save to filesystem
err = ioutil.WriteFile(path.Join(ConfigPath, fileName), lookupValue, fileMode) err = os.WriteFile(path.Join(ConfigPath, fileName), lookupValue, fileMode)
if err != nil { if err != nil {
// we couldn't save the file for some reason // we couldn't save the file for some reason
log.Printf("Hetzner: Failed to write %s:%s %s", fileName, lookupValue, err) log.Printf("Hetzner: Failed to write %s:%s %s", fileName, lookupValue, err)
@ -103,7 +103,7 @@ func hetznerGet(url string) ([]byte, error) {
if resp.StatusCode != 200 { if resp.StatusCode != 200 {
return nil, fmt.Errorf("Hetzner: Status not ok: %d", resp.StatusCode) return nil, fmt.Errorf("Hetzner: Status not ok: %d", resp.StatusCode)
} }
body, err := ioutil.ReadAll(resp.Body) body, err := io.ReadAll(resp.Body)
if err != nil { if err != nil {
return nil, fmt.Errorf("Hetzner: Failed to read http response: %s", err) return nil, fmt.Errorf("Hetzner: Failed to read http response: %s", err)
} }

View File

@ -2,7 +2,7 @@ package main
import ( import (
"fmt" "fmt"
"io/ioutil" "io"
"log" "log"
"net/http" "net/http"
"os" "os"
@ -43,7 +43,7 @@ func (p *ProviderMetaldata) Extract() ([]byte, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
err = ioutil.WriteFile(path.Join(ConfigPath, Hostname), hostname, 0644) err = os.WriteFile(path.Join(ConfigPath, Hostname), hostname, 0644)
if err != nil { if err != nil {
return nil, fmt.Errorf("Metaldata: Failed to write hostname: %s", err) return nil, fmt.Errorf("Metaldata: Failed to write hostname: %s", err)
} }
@ -82,7 +82,7 @@ func (p *ProviderMetaldata) Extract() ([]byte, error) {
func metaldataMetaGet(lookupName string, fileName string, fileMode os.FileMode) { func metaldataMetaGet(lookupName string, fileName string, fileMode os.FileMode) {
if lookupValue, err := metaldataGet(metaldataMetaDataURL + lookupName); err == nil { if lookupValue, err := metaldataGet(metaldataMetaDataURL + lookupName); err == nil {
// we got a value from the metadata server, now save to filesystem // we got a value from the metadata server, now save to filesystem
err = ioutil.WriteFile(path.Join(ConfigPath, fileName), lookupValue, fileMode) err = os.WriteFile(path.Join(ConfigPath, fileName), lookupValue, fileMode)
if err != nil { if err != nil {
// we couldn't save the file for some reason // we couldn't save the file for some reason
log.Printf("Metaldata: Failed to write %s:%s %s", fileName, lookupValue, err) log.Printf("Metaldata: Failed to write %s:%s %s", fileName, lookupValue, err)
@ -111,7 +111,7 @@ func metaldataGet(url string) ([]byte, error) {
if resp.StatusCode != 200 { if resp.StatusCode != 200 {
return nil, fmt.Errorf("Metaldata: Status not ok: %d", resp.StatusCode) return nil, fmt.Errorf("Metaldata: Status not ok: %d", resp.StatusCode)
} }
body, err := ioutil.ReadAll(resp.Body) body, err := io.ReadAll(resp.Body)
if err != nil { if err != nil {
return nil, fmt.Errorf("Metaldata: Failed to read http response: %s", err) return nil, fmt.Errorf("Metaldata: Failed to read http response: %s", err)
} }
@ -129,7 +129,7 @@ func (p *ProviderMetaldata) handleSSH() error {
return fmt.Errorf("Failed to create %s: %s", SSH, err) return fmt.Errorf("Failed to create %s: %s", SSH, err)
} }
err = ioutil.WriteFile(path.Join(ConfigPath, SSH, "authorized_keys"), sshKeys, 0600) err = os.WriteFile(path.Join(ConfigPath, SSH, "authorized_keys"), sshKeys, 0600)
if err != nil { if err != nil {
return fmt.Errorf("Failed to write ssh keys: %s", err) return fmt.Errorf("Failed to write ssh keys: %s", err)
} }

View File

@ -2,7 +2,7 @@ package main
import ( import (
"fmt" "fmt"
"io/ioutil" "io"
"log" "log"
"net/http" "net/http"
"os" "os"
@ -37,7 +37,7 @@ func (p *ProviderOpenstack) Extract() ([]byte, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
err = ioutil.WriteFile(path.Join(ConfigPath, Hostname), hostname, 0644) err = os.WriteFile(path.Join(ConfigPath, Hostname), hostname, 0644)
if err != nil { if err != nil {
return nil, fmt.Errorf("OpenStack: Failed to write hostname: %s", err) return nil, fmt.Errorf("OpenStack: Failed to write hostname: %s", err)
} }
@ -79,7 +79,7 @@ func (p *ProviderOpenstack) Extract() ([]byte, error) {
func openstackMetaGet(lookupName string, fileName string, fileMode os.FileMode) { func openstackMetaGet(lookupName string, fileName string, fileMode os.FileMode) {
if lookupValue, err := openstackGet(metaDataURL + lookupName); err == nil { if lookupValue, err := openstackGet(metaDataURL + lookupName); err == nil {
// we got a value from the metadata server, now save to filesystem // we got a value from the metadata server, now save to filesystem
err = ioutil.WriteFile(path.Join(ConfigPath, fileName), lookupValue, fileMode) err = os.WriteFile(path.Join(ConfigPath, fileName), lookupValue, fileMode)
if err != nil { if err != nil {
// we couldn't save the file for some reason // we couldn't save the file for some reason
log.Printf("OpenStack: Failed to write %s:%s %s", fileName, lookupValue, err) log.Printf("OpenStack: Failed to write %s:%s %s", fileName, lookupValue, err)
@ -108,7 +108,7 @@ func openstackGet(url string) ([]byte, error) {
if resp.StatusCode != 200 { if resp.StatusCode != 200 {
return nil, fmt.Errorf("OpenStack: Status not ok: %d", resp.StatusCode) return nil, fmt.Errorf("OpenStack: Status not ok: %d", resp.StatusCode)
} }
body, err := ioutil.ReadAll(resp.Body) body, err := io.ReadAll(resp.Body)
if err != nil { if err != nil {
return nil, fmt.Errorf("OpenStack: Failed to read http response: %s", err) return nil, fmt.Errorf("OpenStack: Failed to read http response: %s", err)
} }
@ -126,7 +126,7 @@ func (p *ProviderOpenstack) handleSSH() error {
return fmt.Errorf("Failed to create %s: %s", SSH, err) return fmt.Errorf("Failed to create %s: %s", SSH, err)
} }
err = ioutil.WriteFile(path.Join(ConfigPath, SSH, "authorized_keys"), sshKeys, 0600) err = os.WriteFile(path.Join(ConfigPath, SSH, "authorized_keys"), sshKeys, 0600)
if err != nil { if err != nil {
return fmt.Errorf("Failed to write ssh keys: %s", err) return fmt.Errorf("Failed to write ssh keys: %s", err)
} }

View File

@ -2,7 +2,6 @@ package main
import ( import (
"fmt" "fmt"
"io/ioutil"
"net" "net"
"os" "os"
"path" "path"
@ -47,7 +46,7 @@ func (p *ProviderPacket) Extract() ([]byte, error) {
return nil, p.err return nil, p.err
} }
if err := ioutil.WriteFile(path.Join(ConfigPath, Hostname), []byte(p.metadata.Hostname), 0644); err != nil { if err := os.WriteFile(path.Join(ConfigPath, Hostname), []byte(p.metadata.Hostname), 0644); err != nil {
return nil, fmt.Errorf("Packet: Failed to write hostname: %s", err) return nil, fmt.Errorf("Packet: Failed to write hostname: %s", err)
} }
@ -57,7 +56,7 @@ func (p *ProviderPacket) Extract() ([]byte, error) {
sshKeys := strings.Join(p.metadata.SSHKeys, "\n") sshKeys := strings.Join(p.metadata.SSHKeys, "\n")
if err := ioutil.WriteFile(path.Join(ConfigPath, SSH, "authorized_keys"), []byte(sshKeys), 0600); err != nil { if err := os.WriteFile(path.Join(ConfigPath, SSH, "authorized_keys"), []byte(sshKeys), 0600); err != nil {
return nil, fmt.Errorf("Failed to write ssh keys: %s", err) return nil, fmt.Errorf("Failed to write ssh keys: %s", err)
} }
@ -125,7 +124,7 @@ func networkConfig(ni metadata.NetworkInfo) error {
// weirdly creating a bind always seems to return EEXIST // weirdly creating a bind always seems to return EEXIST
fmt.Fprintf(os.Stderr, "Error adding bond0: %v (ignoring)", err) fmt.Fprintf(os.Stderr, "Error adding bond0: %v (ignoring)", err)
} }
if err := ioutil.WriteFile("/sys/class/net/bond0/bonding/mode", []byte(strconv.Itoa(int(ni.Bonding.Mode))), 0); err != nil { if err := os.WriteFile("/sys/class/net/bond0/bonding/mode", []byte(strconv.Itoa(int(ni.Bonding.Mode))), 0); err != nil {
return fmt.Errorf("Cannot write to /sys/class/net/bond0/bonding/mode: %v", err) return fmt.Errorf("Cannot write to /sys/class/net/bond0/bonding/mode: %v", err)
} }
if err := netlink.LinkSetUp(bond); err != nil { if err := netlink.LinkSetUp(bond); err != nil {

View File

@ -5,7 +5,7 @@ import (
"bytes" "bytes"
"errors" "errors"
"fmt" "fmt"
"io/ioutil" "io"
"log" "log"
"net" "net"
"net/http" "net/http"
@ -62,7 +62,7 @@ func (p *ProviderScaleway) Extract() ([]byte, error) {
return nil, fmt.Errorf("Scaleway: Failed to get hostname: %s", err) return nil, fmt.Errorf("Scaleway: Failed to get hostname: %s", err)
} }
err = ioutil.WriteFile(path.Join(ConfigPath, Hostname), hostname, 0644) err = os.WriteFile(path.Join(ConfigPath, Hostname), hostname, 0644)
if err != nil { if err != nil {
return nil, fmt.Errorf("Scaleway: Failed to write hostname: %s", err) return nil, fmt.Errorf("Scaleway: Failed to write hostname: %s", err)
} }
@ -72,7 +72,7 @@ func (p *ProviderScaleway) Extract() ([]byte, error) {
return nil, fmt.Errorf("Scaleway: Failed to get instanceID: %s", err) return nil, fmt.Errorf("Scaleway: Failed to get instanceID: %s", err)
} }
err = ioutil.WriteFile(path.Join(ConfigPath, instanceIDFile), instanceID, 0644) err = os.WriteFile(path.Join(ConfigPath, instanceIDFile), instanceID, 0644)
if err != nil { if err != nil {
return nil, fmt.Errorf("Scaleway: Failed to write instance_id: %s", err) return nil, fmt.Errorf("Scaleway: Failed to write instance_id: %s", err)
} }
@ -82,7 +82,7 @@ func (p *ProviderScaleway) Extract() ([]byte, error) {
return nil, fmt.Errorf("Scaleway: Failed to get instanceLocation: %s", err) return nil, fmt.Errorf("Scaleway: Failed to get instanceLocation: %s", err)
} }
err = ioutil.WriteFile(path.Join(ConfigPath, instanceLocationFile), instanceLocation, 0644) err = os.WriteFile(path.Join(ConfigPath, instanceLocationFile), instanceLocation, 0644)
if err != nil { if err != nil {
return nil, fmt.Errorf("Scaleway: Failed to write instance_location: %s", err) return nil, fmt.Errorf("Scaleway: Failed to write instance_location: %s", err)
} }
@ -92,7 +92,7 @@ func (p *ProviderScaleway) Extract() ([]byte, error) {
// not an error // not an error
log.Printf("Scaleway: Failed to get publicIP: %s", err) log.Printf("Scaleway: Failed to get publicIP: %s", err)
} else { } else {
err = ioutil.WriteFile(path.Join(ConfigPath, publicIPFile), publicIP, 0644) err = os.WriteFile(path.Join(ConfigPath, publicIPFile), publicIP, 0644)
if err != nil { if err != nil {
return nil, fmt.Errorf("Scaleway: Failed to write public_ip: %s", err) return nil, fmt.Errorf("Scaleway: Failed to write public_ip: %s", err)
} }
@ -104,7 +104,7 @@ func (p *ProviderScaleway) Extract() ([]byte, error) {
return nil, fmt.Errorf("Scaleway: Failed to get privateIP: %s", err) return nil, fmt.Errorf("Scaleway: Failed to get privateIP: %s", err)
} }
err = ioutil.WriteFile(path.Join(ConfigPath, privateIPFile), privateIP, 0644) err = os.WriteFile(path.Join(ConfigPath, privateIPFile), privateIP, 0644)
if err != nil { if err != nil {
return nil, fmt.Errorf("Scaleway: Failed to write private_ip: %s", err) return nil, fmt.Errorf("Scaleway: Failed to write private_ip: %s", err)
} }
@ -152,7 +152,7 @@ func scalewayGet(url string) ([]byte, error) {
if resp.StatusCode != 200 { if resp.StatusCode != 200 {
return nil, fmt.Errorf("Scaleway: Status not ok: %d", resp.StatusCode) return nil, fmt.Errorf("Scaleway: Status not ok: %d", resp.StatusCode)
} }
body, err := ioutil.ReadAll(resp.Body) body, err := io.ReadAll(resp.Body)
if err != nil { if err != nil {
return nil, fmt.Errorf("Scaleway: Failed to read http response: %s", err) return nil, fmt.Errorf("Scaleway: Failed to read http response: %s", err)
} }
@ -192,7 +192,7 @@ func scalewayGetUserdata() ([]byte, error) {
} }
defer resp.Body.Close() defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body) body, err := io.ReadAll(resp.Body)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -225,7 +225,7 @@ func (p *ProviderScaleway) handleSSH(metadata []byte) error {
return fmt.Errorf("Failed to create %s: %s", SSH, err) return fmt.Errorf("Failed to create %s: %s", SSH, err)
} }
err = ioutil.WriteFile(path.Join(ConfigPath, SSH, "authorized_keys"), []byte(rootKeys), 0600) err = os.WriteFile(path.Join(ConfigPath, SSH, "authorized_keys"), []byte(rootKeys), 0600)
if err != nil { if err != nil {
return fmt.Errorf("Failed to write ssh keys: %s", err) return fmt.Errorf("Failed to write ssh keys: %s", err)
} }

View File

@ -2,7 +2,7 @@ package main
import ( import (
"fmt" "fmt"
"io/ioutil" "io"
"log" "log"
"net/http" "net/http"
"os" "os"
@ -41,7 +41,7 @@ func (p *ProviderVultr) Extract() ([]byte, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
err = ioutil.WriteFile(path.Join(ConfigPath, Hostname), hostname, 0644) err = os.WriteFile(path.Join(ConfigPath, Hostname), hostname, 0644)
if err != nil { if err != nil {
return nil, fmt.Errorf("Vultr: Failed to write hostname: %s", err) return nil, fmt.Errorf("Vultr: Failed to write hostname: %s", err)
} }
@ -73,7 +73,7 @@ func (p *ProviderVultr) Extract() ([]byte, error) {
func vultrMetaGet(lookupName string, fileName string, fileMode os.FileMode) { func vultrMetaGet(lookupName string, fileName string, fileMode os.FileMode) {
if lookupValue, err := vultrGet(vultrMetaDataURL + lookupName); err == nil { if lookupValue, err := vultrGet(vultrMetaDataURL + lookupName); err == nil {
// we got a value from the metadata server, now save to filesystem // we got a value from the metadata server, now save to filesystem
err = ioutil.WriteFile(path.Join(ConfigPath, fileName), lookupValue, fileMode) err = os.WriteFile(path.Join(ConfigPath, fileName), lookupValue, fileMode)
if err != nil { if err != nil {
// we couldn't save the file for some reason // we couldn't save the file for some reason
log.Printf("Vultr: Failed to write %s:%s %s", fileName, lookupValue, err) log.Printf("Vultr: Failed to write %s:%s %s", fileName, lookupValue, err)
@ -102,7 +102,7 @@ func vultrGet(url string) ([]byte, error) {
if resp.StatusCode != 200 { if resp.StatusCode != 200 {
return nil, fmt.Errorf("Vultr: Status not ok: %d", resp.StatusCode) return nil, fmt.Errorf("Vultr: Status not ok: %d", resp.StatusCode)
} }
body, err := ioutil.ReadAll(resp.Body) body, err := io.ReadAll(resp.Body)
if err != nil { if err != nil {
return nil, fmt.Errorf("Vultr: Failed to read http response: %s", err) return nil, fmt.Errorf("Vultr: Failed to read http response: %s", err)
} }
@ -120,7 +120,7 @@ func (p *ProviderVultr) handleSSH() error {
return fmt.Errorf("Failed to create %s: %s", SSH, err) return fmt.Errorf("Failed to create %s: %s", SSH, err)
} }
err = ioutil.WriteFile(path.Join(ConfigPath, SSH, "authorized_keys"), sshKeys, 0600) err = os.WriteFile(path.Join(ConfigPath, SSH, "authorized_keys"), sshKeys, 0600)
if err != nil { if err != nil {
return fmt.Errorf("Failed to write ssh keys: %s", err) return fmt.Errorf("Failed to write ssh keys: %s", err)
} }

View File

@ -129,7 +129,7 @@ func makeDevLinks() error {
} }
} }
devs, err := ioutil.ReadDir("/sys/class/block") devs, err := os.ReadDir("/sys/class/block")
if err != nil { if err != nil {
return err return err
} }

View File

@ -4,7 +4,6 @@ import (
"bytes" "bytes"
"flag" "flag"
"fmt" "fmt"
"io/ioutil"
"log" "log"
"os" "os"
"path/filepath" "path/filepath"
@ -54,13 +53,13 @@ func splitKv(r rune) bool {
func main() { func main() {
flag.Parse() flag.Parse()
files, err := ioutil.ReadDir(configDir) files, err := os.ReadDir(configDir)
if err != nil { if err != nil {
log.Fatalf("Cannot read directory %s: %s", configDir, err) log.Fatalf("Cannot read directory %s: %s", configDir, err)
} }
for _, file := range files { for _, file := range files {
contents, err := ioutil.ReadFile(filepath.Join(configDir, file.Name())) contents, err := os.ReadFile(filepath.Join(configDir, file.Name()))
if err != nil { if err != nil {
log.Fatalf("Cannot read file %s: %s", file.Name(), err) log.Fatalf("Cannot read file %s: %s", file.Name(), err)
} }

View File

@ -4,7 +4,6 @@ import (
"bytes" "bytes"
"flag" "flag"
"fmt" "fmt"
"io/ioutil"
"log" "log"
"os" "os"
"path/filepath" "path/filepath"
@ -48,13 +47,13 @@ func sysfs(line []byte) error {
func main() { func main() {
flag.Parse() flag.Parse()
files, err := ioutil.ReadDir(configDir) files, err := os.ReadDir(configDir)
if err != nil { if err != nil {
log.Fatalf("Cannot read directory %s: %s", configDir, err) log.Fatalf("Cannot read directory %s: %s", configDir, err)
} }
for _, file := range files { for _, file := range files {
contents, err := ioutil.ReadFile(filepath.Join(configDir, file.Name())) contents, err := os.ReadFile(filepath.Join(configDir, file.Name()))
if err != nil { if err != nil {
log.Fatalf("Cannot read file %s: %s", file.Name(), err) log.Fatalf("Cannot read file %s: %s", file.Name(), err)
} }