TextSQL

From Cfwiki

Jump to: navigation, search

The idea of this page is to document TextSQL, a proposed replacement for the editfiles section of cfengine. It's also suggested that it be a separate language, but embedded in the cfengine source using the other suggested feature, Action Templating, documented in Templating Ideas. This idea was originally suggested in a mailing list post here by Tim Nelson.

Contents

Basic Structure

The idea is that text files, and in particular, configuration files, be dealt with in a fashion similar to SQL. However, since there are a much wider range of actions, we'll put the actions *after* the WHERE clause. An example:

WHERE
        CurrentBlock = "service amanda"
        AND Field(0) = "disable"
DO
        Field(1) = yes

Types

  • Int
  • String
  • Regex (like string, but interacts with operators differently)
  • StringLike (Regex or String)
  • Array (When evaluated in a scalar context, returns Array[0])
  • Separator
    • Str (String to insert when inserting)
    • RE (Regex for splitting things apart; defaults to "\QStr\E")
  • PositionalString
    • Contents (String)
    • Position (Int)
  • File
    • Contents (Array)
    • Exists (Int)
    • ModifiedTime (Int)

Operators

The following are in no particular order. "linecondition" == what is returned by Line Comparative Operators


  • Line Comparative Operators
    • = Equals (strcmp on strings, PCRE on regexes)
    • > Greater Than (Int)
    • Contains (strstr on strings, PCRE on regexes?)
  • Other Comparative Operators
    • Defined <variable>
    • Exists <linecondition>
  • Arithmetic Operators
    • + Add (strcat, plus)
  • Assignment Operators
    •  := +=
  • Array Operators (in WHERE, do <action> for all <x>, in DO, just return Array)
    • .. Range ( <linecondition1> .. <linecondition2> returns Array)
    • All <linecondition>
    • Unshift, Shift (see Perl)
  • Parenthetic Operators
    • ()
  • Boolean Operators
    • AND, OR, NOT

Variables

Structure Variables

Structure variables define the structure of the file.

Variable Type Default Value Comments
CommentStart Separator "#"
CommentEnd Separator NULL (equivalent to LineSeparator)
BlockStart Separator "<$1 $2>", /^<(\w+)\s+(.*?)>$/
BlockEnd Separator "</$1>", /^<\/(\w+)>$/
BlockNamer String "" (defaults to $1 from BlockStart) Could be set to WHERE Option = "ServerName" DO CurrentBlock = Field[1]
LineSeparator Separator "\n"
FieldSeparator[] Array of Separator " ", /\s+/ For /etc/password
FieldSeparator.Str := ':'

FieldSeparator[1].Str := ','

FieldMax[] Array of Int 0 (infinite)

Magic Variables

Magic Variables are variables which are automatically updated as a side effect of other commands.

  • CurrentLine[] (Array of PositionalString); a new CurrentLine context does a perl-unshift on the array, the end of a context causes a perl-shift on the array
  • CurrentGroup[] (PositionalString); The 0th element is the innermost group
  • CurrentFile (File)
  • LastLine[] (Array of ???; we want CurrentFile, and the Group names)
  • Field[] (Array of PositionalString)
  • Option[] (alias for Field, above; intended so that people can say "When Option = "ServerName", etc)

Other Variables

  •  ??? Repository
  • Syslog
  • Edited: True if the file was edited
  • EditType: Defaults to ASCII, but can be Binary
  • AutoAbort (StringLike)

Actions

Group Actions

  • AbortGroup <condition>
  • BeginGroup <condition>
  • EndGroup [ CatchAbort ] (If CatchAbort is specified, it catches the abort)

File Editing Actions

Many of the actions from the old cfengine editfiles can be accomplished by setting CurrentLine.Contents and Field(x).Contents.

  • Comment/Uncomment (implies CurrentLine; cf. WHERE clause)
  • Delete (implies CurrentLine; cf WHERE clause)
  • Insert <Array of String>
  • FixEndOfLine (CurrentLine)

Evil commands people shouldn't need, but retained as a stopgap until we know why people need them:

  • InsertLine <String> [ Before | After ]
  • AppendLine <String>
  • PrependLine <String>

Misc Actions

  • AddToReturn <variable>; adds a variable to the list of variables to return; this will define variables or classes in cfengine, after this TextSQL run is finished
  • Warn
  •  ??? Elapsed (IfElapsed)
  •  ??? ExpireAfter
  • ResetVariables; Resets Structure variables to their default settings
  • SetFileType <string>; Performs a ResetVariables, and then loads in a set of filetype settings from a config file. The intention would be to have the following structures available (thanks to Holger Schurig for this idea):
    • ASCII
      • Flat (just line-based; can't think of an example)
      • Key-Value
        • Shell (mostly just variables, which are then evaluated by eg. bash; no spaces around equals)
        • INI (like Windows INI, KDE, MySQL, etc); blocks
        • ISC (bind, dhcpd, xinetd, etc); blocks
      • Table-like
        • Blank Separate (/etc/fstab)
        • System Maps (/etc/passwd)
        • Apache-Like; blocks
    • binary (no format, limited control set)

Editfiles remains

This is a list of the things that would remain a part of editfiles. Mostly they have to do with selecting files and the like.

I didn't know what to do with the Repository variable.

File Selection Actions

  • AutoCreate
  • AutoMountDirectResources
  • Filter
  • Recurse
  • Umask

Action Templating actions

The following actions will be be part of or be replaced by the Action Templating system.

  • Backup
  • RunScript
  • SetScript
  • Umask
  • UseShell
Personal tools