2018-02-21 13:29:18 +03:00
package generators
import (
2018-03-13 19:22:36 +03:00
"io/ioutil"
2018-02-21 13:29:18 +03:00
"os"
"path/filepath"
2018-03-13 19:22:36 +03:00
"strings"
2018-02-21 13:29:18 +03:00
"github.com/lxc/distrobuilder/image"
2018-03-07 18:37:25 +03:00
"github.com/lxc/distrobuilder/shared"
2018-03-13 19:22:36 +03:00
lxd "github.com/lxc/lxd/shared"
2018-02-22 18:32:43 +03:00
"github.com/lxc/lxd/shared/api"
2018-02-21 13:29:18 +03:00
)
// HostsGenerator represents the hosts generator.
type HostsGenerator struct { }
2018-03-07 18:37:25 +03:00
// RunLXC creates a LXC specific entry in the hosts file.
func ( g HostsGenerator ) RunLXC ( cacheDir , sourceDir string , img * image . LXCImage ,
defFile shared . DefinitionFile ) error {
2018-03-13 19:22:36 +03:00
// Skip if the file doesn't exist
if ! lxd . PathExists ( filepath . Join ( sourceDir , defFile . Path ) ) {
return nil
}
2018-02-22 18:28:26 +03:00
// Store original file
2018-03-07 18:37:25 +03:00
err := StoreFile ( cacheDir , sourceDir , defFile . Path )
2018-02-21 13:29:18 +03:00
if err != nil {
return err
}
2018-03-13 19:22:36 +03:00
// Read the current content
content , err := ioutil . ReadFile ( filepath . Join ( sourceDir , defFile . Path ) )
2018-02-21 13:29:18 +03:00
if err != nil {
return err
}
2018-03-13 19:22:36 +03:00
// Replace hostname with placeholder
content = [ ] byte ( strings . Replace ( string ( content ) , "distrobuilder" , "LXC_NAME" , - 1 ) )
// Add a new line if needed
if ! strings . Contains ( string ( content ) , "LXC_NAME" ) {
content = append ( [ ] byte ( "127.0.1.1\tLXC_NAME\n" ) , content ... )
}
// Overwrite the file
err = ioutil . WriteFile ( filepath . Join ( sourceDir , defFile . Path ) , content , 0644 )
if err != nil {
return err
}
2018-02-21 13:29:18 +03:00
2018-02-22 18:28:26 +03:00
// Add hostname path to LXC's templates file
2018-03-07 18:37:25 +03:00
return img . AddTemplate ( defFile . Path )
2018-02-21 13:29:18 +03:00
}
2018-03-07 18:37:25 +03:00
// RunLXD creates a hosts template.
func ( g HostsGenerator ) RunLXD ( cacheDir , sourceDir string , img * image . LXDImage ,
defFile shared . DefinitionFile ) error {
2018-03-13 19:22:36 +03:00
// Skip if the file doesn't exist
if ! lxd . PathExists ( filepath . Join ( sourceDir , defFile . Path ) ) {
return nil
}
2018-02-21 13:29:18 +03:00
templateDir := filepath . Join ( cacheDir , "templates" )
2018-02-22 18:28:26 +03:00
// Create templates path
2018-02-21 13:29:18 +03:00
err := os . MkdirAll ( templateDir , 0755 )
if err != nil {
return err
}
2018-03-13 19:22:36 +03:00
// Read the current content
content , err := ioutil . ReadFile ( filepath . Join ( sourceDir , defFile . Path ) )
2018-02-21 13:29:18 +03:00
if err != nil {
return err
}
2018-03-13 19:22:36 +03:00
// Replace hostname with placeholder
content = [ ] byte ( strings . Replace ( string ( content ) , "distrobuilder" , "{{ container.name }}" , - 1 ) )
2018-03-14 21:52:33 +03:00
// Add a new line if needed
if ! strings . Contains ( string ( content ) , "{{ container.name }}" ) {
content = append ( [ ] byte ( "127.0.1.1\t{{ container.name }}\n" ) , content ... )
}
2018-03-13 19:22:36 +03:00
// Write the template
err = ioutil . WriteFile ( filepath . Join ( templateDir , "hosts.tpl" ) , content , 0644 )
2018-02-21 13:29:18 +03:00
if err != nil {
return err
}
2018-03-07 18:37:25 +03:00
img . Metadata . Templates [ defFile . Path ] = & api . ImageMetadataTemplate {
2018-02-22 18:32:43 +03:00
Template : "hosts.tpl" ,
2018-02-21 13:29:18 +03:00
When : [ ] string {
"create" ,
"copy" ,
} ,
}
return err
}
2018-03-08 19:02:25 +03:00
// Run does nothing.
func ( g HostsGenerator ) Run ( cacheDir , sourceDir string ,
defFile shared . DefinitionFile ) error {
return nil
}